我在 ajax 调用中有一些代码,如下所示
$.ajax({
url: query_string,
cache: true,
type: 'GET',
async: false, // must be set to false
success: function (data, success) {
alert(jquery.parseJSON(data));
alert('success');
//alert(data);
//alert(success);
},
dataType: 'jsonp',
error :function( jqxhr, textStatus, error ) {
var err = textStatus + ', ' + error;
alert(err);
},
complete: function (jqxhr, textStatus ){
//alert( "complete: " + JSON.stringify(jqxhr)+" "+ textStatus );
}
});
当我将此代码运行到带有 fire bug 的 firefox 中时。 firebug 以 perect json 格式显示响应,但在 firebug 中显示以下错误
SyntaxError: missing ; before statement
{"products":[{"title":"xyz","id":1718,"created_at
那我该如何解决呢??
最佳答案
你告诉 jQuery 将响应作为 JSONP 处理:
dataType: 'jsonp'
... 但响应是 JSON,而不是 JSONP。
去掉 p
或完全去掉 dataType
行,让 jQuery 从 Content-Type
中确定数据类型响应(应该是 application/json
)。
关于javascript - 如何解决json错误: SyntaxError: missing ; before statement {"products":[ {"title" :"xyz" ,"id":1718,“created_at?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014342/