“意外的令牌o”使用$ .parseJSON时 [英] "Unexpected token o" when using $.parseJSON
本文介绍了“意外的令牌o”使用$ .parseJSON时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下ajax调用给出以下结果:
The following ajax call gives the following result:
$.ajax({
type: "POST",
url: //**My full URL goes here**,
data: {sources: sources},
dataType: "json",
success: function(data) {
alert(data);
alert(data.length);
for (var i = 0; i < data.length; i++)
{
alert(data[i]);
}
}
});
结果:
data:
[objject object],[objject object],[objject object]
length:
3
in loop:
[objject object]
[objject object]
[objject object]
以及我刚刚添加的以下代码:
and the following code , in which I just added:
var data = $.parseJSON(data);
$.ajax({
type: "POST",
url: //**My full URL goes here**,
data: {sources: sources},
dataType: "json",
success: function(data) {
var data = $.parseJSON(data);
alert(data);
alert(data.length);
for (var i = 0; i < data.length; i++)
{
alert(data[i]);
}
}
});
上面的代码给出了以下错误:
The above code gives me the following error:
未捕获的语法错误:意外的令牌o
Uncaught Syntax Error: Unexpected token o
为什么会这样?难道我做错了什么?我该如何解决?
Why is that? Am I doing something wrong? How can I fix it?
推荐答案
数据
已经过解析对象,因为你传递 dataType:'json'
,所以不需要再次解析它。
The data
is already a parsed object since you are passing dataType: 'json'
, so there is no need to parse it again.
再次要调试和检查数据的值,请改用控制台日志记录 alert()
,如 console.log(data)
Again to debug and inspect the value of data, use console logging instead of alert()
, like console.log(data)
这篇关于“意外的令牌o”使用$ .parseJSON时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文