Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未捕获的语法错误:parseJSON方法javascript中的输入意外结束_Javascript_Jquery_Json - Fatal编程技术网

未捕获的语法错误:parseJSON方法javascript中的输入意外结束

未捕获的语法错误:parseJSON方法javascript中的输入意外结束,javascript,jquery,json,Javascript,Jquery,Json,在使用javascript的网页中,我使用 $("#waypt_sel").val(JSON.stringify(itins.arr_intin)); 然后使用 waypts_input = $.parseJSON($("#waypt_sel").val()) 这是可行的,只是有时它会给 未捕获的语法错误:输入意外结束 。我通过json解析将错误跟踪到这一行。但我很困惑,因为它有时对相同的字符串有效,但有时对相同的字符串无效 我检查了传递给html输入的值,它对相同的值起作用和不起作用。 下

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

$("#waypt_sel").val(JSON.stringify(itins.arr_intin));
然后使用

waypts_input = $.parseJSON($("#waypt_sel").val())
这是可行的,只是有时它会给

未捕获的语法错误:输入意外结束

。我通过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、 回调/j()jquery-1.11.3.min.js:2

m、 回调/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

提前感谢。

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

您使用了什么json工具


您在一个工具中序列化,然后用另一个工具反序列化,可能会出现这种情况。

问题是我使用异步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
。但是这可能会导致浏览器冻结,因此不推荐使用。

您可以展示一个
JSON.stringify(itins.arr_intin)
的示例吗?展示代码,展示JSON,制作一个工作片段/小提琴。更多信息:我们不是水晶球的奇才。我知道这并不能回答问题,但为什么不将值存储在一个简单的javascript变量中呢?“我很困惑,因为这有时有效,但有时不适用于相同的字符串。”-我真的不敢相信。为什么不将变量传递到一个值中呢?这让我很困惑<代码>\只是字符串文字的一部分。这是因为这个json数组是另一个更大的json数组的一部分。在json数组中使用json数组时需要转义双引号,否则它会给我errors@NisalUpendra:在这种情况下,它实际上是一个反模式。不要将JSON放在JSON中的JSON字符串中。