【问题标题】:Uncaught SyntaxError: Unexpected end of input in parseJSON method javascript未捕获的 SyntaxError:parseJSON 方法 javascript 中的输入意外结束
【发布时间】:2015-11-30 09:51:01
【问题描述】:

在使用 javascript 的网页中,我使用

将数据传递到隐藏的输入字段
$("#waypt_sel").val(JSON.stringify(itins.arr_intin));

然后使用

访问该数据
waypts_input = $.parseJSON($("#waypt_sel").val())

这可行,只是有时它会给出一个

Uncaught SyntaxError: Unexpected end of input

。我使用 json 解析跟踪到这一行的错误。但我很困惑,因为这有时有效,但有时它不适用于相同的相同字符串。

我检查了传递给 html 输入的值,它适用于相同的值,但不适用于相同的值。 这是我传递的 json 字符串的示例:

"[{\"location\":\"8.3353156, 80.3329846\",\"stopover\":true}, {\"location\":\"8.0326424, 80.7446666\",\"stopover\":true}, {\"location\":\"7.9577778, 80.667518\",\"stopover\":true}, {\"location\":\"7.953208, 81.006675\",\"stopover\":true}, {\"location\":\"7.885949, 80.651479\",\"stopover\":true},{\"location\":\"7.2905425, 80.5986581\",\"stopover\":true},{\"location\":\"7.300322, 80.386362\",\"stopover\":true}]"

这是我使用的代码结构。

$(document).ready(function() {


    $.ajax({
        url: "aa.php",
        type: "POST",
        data: {
            id: selected_i
        },
        success: function(result) {

            itins = $.parseJSON(result);
            $("#waypt_sel").val(JSON.stringify(itins.arr_intin));
        }
    });


    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "dd.php",

        success: function(result) {
            locations = $.parseJSON(result);
            initializeMap();

        }
    });


    function initializeMap() {
    //other code
        calculateAndDisplayRoute();
//other code


        function calculateAndDisplayRoute() {
            //other code
            waypts_input = $.parseJSON($("#waypt_sel").val());
            waypts_json_input = $.parseJSON(waypts_input);
            //other code
            }

    }


});

这是我在 Firefox 开发者版浏览器上收到的详细错误消息。

SyntaxError:JSON.parse:第 1 行第 1 列的数据意外结束 JSON 数据

calculateAndDisplayRoute() map.js:366

initializeMap() map.js:290

.success() map.js:62

m.Callbacks/j() jquery-1.11.3.min.js:2

m.Callbacks/k.fireWith() jquery-1.11.3.min.js:2

x() jquery-1.11.3.min.js:5

.send/b() jquery-1.11.3.min.js:5

提前致谢。

【问题讨论】:

  • 你能举一个JSON.stringify(itins.arr_intin)的例子吗?
  • 显示代码,显示 json,制作一个有效的 sn-p/fiddle。查看更多:blog.stackoverflow.com/2014/09/…我们不是拥有水晶球的巫师。
  • 我知道这并不能回答问题,但你为什么不将值存储在一个简单的 javascript 变量中?
  • "我很困惑,因为这有时有效,但有时它不适用于相同的字符串。" - 我真的不敢相信。
  • 为什么不直接将变量传入一个值?现在这让我很困惑!

标签: javascript jquery json


【解决方案1】:

在javascript中反序列化json字符串时不需要“\”。

你使用了哪些 json 工具?

你用一个工具序列化,用另一个工具反序列化,可能会得到这个场景。

【讨论】:

  • \ 只是字符串文字的一部分。
  • 这是因为这个 json 数组是另一个更大的 json 数组的一部分。在 json 数组中使用 json 数组时需要转义双引号,否则会报错
  • @NisalUpendra:那么在这种情况下,它确实是一种反模式。不要将 JSON 放在 JSON 中的 JSON 字符串中。
【解决方案2】:

问题是我使用异步 ajax 请求来检索 json 数据。当数据被检索并粘贴到 html 时,已经执行了使用 html 数据的代码,因此出现了错误。我为 ajax 查询使用了回调函数,这完成了工作。

function get_det(callback) {//Your asynchronous request.  
    $.ajax({
      url: "aa.php",
      type: "POST",
      success: function (result) {
        alert("1st call");
        callback();//invoke when get response 
      }
    });
  }

在调用它的代码中:

get_det(secondFunction);//calling with callback function
function secondFunction()//your callback function
{
 alert("2nd Call");
}

或者,您也可以在 ajax 查询参数中尝试 async: false。但这会导致浏览器死机,不推荐使用。

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2017-01-05
    • 2020-01-08
    • 1970-01-01
    • 2017-09-07
    相关资源
    最近更新 更多