如果我仔细阅读其他答案中的错误评论,real问题在于您在将 json 字符串传递给之前对其进行了字符串化data
. data
期望一个String
包含查询(例如"a=asdf&b=qwer"
or an Object
包含将转换为查询的键和值)。相反,你传递的是类似的东西'{"a":"asdf","b":"qwer"}'
, a String
包含不是查询的字符串化数组。它以某种方式设法转换服务器可以理解的数据,但显然也会触发该错误/功能。
解决方案1
如果您想通过以下方式访问数据$_POST['a']
拿到钥匙"a"
在你的 JSON 对象中:
$.ajax( {
async: false,
data: model, //Pass the Array itself to the function
dataType: "json",
type: "post",
url: serverSideURL
});
(来源;牛仔关于 jQuery 错误)
解决方案2
如果你想检索 JSON 字符串:
$.ajax( {
async: false,
data: {
"MyLilString": JSON.stringify( model )
}
dataType: "json",
type: "post",
url: serverSideURL
});
在这种情况下$_POST["MyLilString"]
包含序列化的 JSON,您可以使用它json_decode()
on.
(来源; Ajpiano 谈 jQuery 错误)
另一个建议
我从评论中提取的另一个建议(但我现在找不到-_-')是设置jsonp
to false
.
$.ajax( {
async: false,
data: model
dataType: "json",
type: "post",
url: serverSideURL,
jsonp: false
});
这应该会阻止 jQuery 将回调函数插入到请求正文中。