【问题标题】:Requested JSON parse failed请求的 JSON 解析失败
【发布时间】:2012-10-23 14:58:32
【问题描述】:

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

【问题讨论】:

  • 在这个js代码中解析错误,或者在服务器上生成的json文本中?
  • 您是否尝试过删除 index.php 上的前导 /?
  • 不要告诉我们确切的问题是什么。它会带走所有猜测的乐趣!每个人都知道工程师喜欢猜测。
  • 你能调试输出你的order变量吗?
  • 好的,所以 index.php 中的 JSON 是错误的。这意味着您发布的代码与问题无关。您可以发布 JSON 响应或 index.php 吗?你用什么浏览器?

标签: jquery ajax json parsing


【解决方案1】:

data: { json: order } ...格式不正确...

【讨论】:

【解决方案2】:

此 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 来自问题var order = [];
猜你喜欢
  • 1970-01-01
  • 2021-03-20
  • 2023-03-29
  • 1970-01-01
  • 2018-11-18
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多