jQuery 请求的 JSON 解析失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13033374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Requested JSON parse failed
提问by holyredbeard
By some reason there's a parsing error with the ajax code below. How could I find out what it is, and/or can someone see what's wrong?
由于某种原因,下面的 ajax 代码存在解析错误。我怎么能找出它是什么,和/或有人能看出哪里出了问题?
$('#listElements').sortable({
//revert: true,
update: function(event, ui) {
var order = [];
$('.listObject li').each(function (e) {
order.push($(this).attr('id'));
});
$.ajax({
type: "POST",
url: "index.php?",
dataType: "json",
data: { json: order }, error: function(jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
}
采纳答案by sataniccrow
data: { json: order } ... it's not well formatted...
数据:{ json: order } ...格式不正确...
回答by PiTheNumber
There is no parsing error in this JavaScript code.
此 JavaScript 代码中没有解析错误。
Please post the response of "index.php" and the error message you got.
请发布“index.php”的响应和您收到的错误消息。
Have a look at the response data. Open index.php in the browser, press F12 and insert this into the console:
看看响应数据。在浏览器中打开 index.php,按 F12 并将其插入控制台:
$.ajax({
type: "POST",
url: "index.php",
//dataType: "json",
data: { json: order },
success: function(data) {
console.log(data);
}
});