Javascript GSON不是对象错误
我有以下json对象:Javascript GSON不是对象错误,javascript,json,gson,Javascript,Json,Gson,我有以下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.graphNo
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"
};
但我还是犯了同样的错误
当您想要在XHR请求中发送JSON对象时,您需要首先字符串化JSON对象
我认为您应该使用
jsonData:cNGJSON
$.ajax({
url: 'validate',
type: 'post',
dataType: 'json',
success: function (data) {
console.log("Success!!");
},
data: JSON.stringify(cNGJSON)
});