0

我有以下 json 对象:

cNGJSON = {
            "one": graph1.graphNode, "two": graph2.graphNode, "three": graph3.graphNode, "four": graph4.graphNode, "five": graph5.graphNode,"six": graph6.graphNode,
            "seven": graph7.graphNode,"eight": graph8.graphNode, "nine": graph9.graphNode,"ten": graph10.graphNode, "eleven": graph11.graphNode, "twelve": graph12.graphNode,
            "thirteen": graph13.graphNode,"fourteen": graph14.graphNode,"fifteen": graph15.graphNode,"sixteen": graph16.graphNode, "seventeen": graph17.graphNode, "eighteen": graph18.graphNode,
            "nineteen": graph19.graphNode   
    };

其中 graph1.graphNode 是一个整数数组。

[1,2,3,4]

我使用 jQuery 将其发送到服务器:

  $.ajax({
            url: 'validate',
            type: 'post',
            dataType: 'json',
            success: function (data) {
                console.log("Success!!");
            },
            data: cNGJSON
        });

但是我得到一个

Expected BEGIN_OBJECT but was STRING at line 1 column 1

每次尝试都会出错。

我尝试将 cNGJSON 设置为:

cNGJSON = { 
            "one": "Number one", "two": "Number two"
    };

我仍然得到同样的错误。

4

1 回答 1

2

当您想在 XHR 请求中发送 JSON 对象时,您需要先对 JSON 对象进行字符串化

$.ajax({
     url: 'validate',
     type: 'post',
     dataType: 'json',
     success: function (data) {
         console.log("Success!!");
     },
     data: JSON.stringify(cNGJSON)
});
于 2013-09-30T17:10:17.953 回答