尝试从javascript加载json时出错

时间:2016-09-14 16:16:22

标签: javascript json ag-grid

我的javascript存在问题。我试图像这样将json数组加载到ag-grid,下面的工作正常,因为我只是从json文件中加载json:

    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', '../dist/output.json');
    httpRequest.send();
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var httpResult = JSON.parse(httpRequest.responseText);


            function isNumeric(n) { 
                return !isNaN(parseFloat(n)) && isFinite(n);
            }

            var parsedData = httpResult.map(function(obj) {
                return Object.keys(obj).reduce(function(memo, key) {
                    var value = obj[key];
                    memo[key] = isNumeric(value) ? Number(value) : value;

                    return memo;
                }, {})
            })

但是,当我按照以下方式执行此操作时(即,从jsp获取json数组,console.log(jsonArray)显示正常,但我收到错误:

var jsonArray = document.getElementById("jsonArray");
        console.log(jsonArray);
        var httpRequest = new XMLHttpRequest();
        httpRequest.open('GET', jsonArray);
        httpRequest.send();
        httpRequest.onreadystatechange = function() {
            if (httpRequest.readyState == 4 && httpRequest.status == 200) {
                var httpResult = JSON.parse(httpRequest.responseText);


                function isNumeric(n) { 
                    return !isNaN(parseFloat(n)) && isFinite(n);
                }

                var parsedData = httpResult.map(function(obj) {
                    return Object.keys(obj).reduce(function(memo, key) {
                        var value = obj[key];
                        memo[key] = isNumeric(value) ? Number(value) : value;

                        return memo;
                    }, {})
                })

我收到以下错误:

Failed to load resource: the server responded with a status of 404 (Not Found)

如何更改第一个代码,以便我可以正确读取jsonArray,它包含所有json数据。

更新:

jsonArray是jsp中的以下内容:

JSONArray jsonArray = new JSONArray(orderDetailsList1);

<input type="hidden" value="<%out.println(jsonArray);%>" id="jsonArray"/>

1 个答案:

答案 0 :(得分:0)

jsonArray是一个DOM对象。要访问其value属性,请使用value属性:

httpRequest.open('GET', jsonArray.value);