【发布时间】: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