请求的JSON解析失败。

3

由于某些原因,下面的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);
                    }
                }
            });
        }

1
在这段 JavaScript 代码中出现了解析错误,还是在服务器端生成的 JSON 文本中出现了错误? - Marc B
你尝试过在index.php前面去掉开头的/吗? - user1090077
4
不要告诉我们确切的问题是什么,这会夺走猜测的乐趣!每个人都知道工程师喜欢猜测。 - Pekka
1
好的,index.php返回的JSON格式不正确。这意味着您发布的代码与问题无关。您能否发布JSON响应或index.php文件?您使用的是什么浏览器? - PiTheNumber
我使用Chrome浏览器。如何获取JSON响应? - holyredbeard
显示剩余6条评论
2个回答

1

这段 JavaScript 代码没有解析错误。

请发布“index.php”的响应和您收到的错误消息。

查看响应数据。在浏览器中打开 index.php,按 F12 并将其插入控制台:

       $.ajax({
            type: "POST",
            url: "index.php",
            //dataType: "json",
            data: { json: order },
            success: function(data) {
               console.log(data);
            }
        });

这里的数据顺序是什么:{json:order} - Sabarish
@Sabarish 这是来自问题 var order = []; - PiTheNumber

1

数据:{ json: 订单 } ... 格式不规范...


我认为这可能会有帮助:https://dev59.com/eG025IYBdhLWcg3wT0Lq - sataniccrow
再多解释一点! - GntS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接