简体   繁体   English

有人请告诉我$ .parseJSON有什么问题[javascript / jquery]

[英]Someone please tell me what is wrong with my $.parseJSON [javascript / jquery]

Here is my tested result. 这是我的测试结果。 I run my app, I get the "Hello" alert. 我运行我的应用程序,收到"Hello"警报。 But I don't get "After parse JSON" alert. 但我没有收到"After parse JSON"警报。

When I comment out the var rstList = $.parseJSON(data); 当我注释掉var rstList = $.parseJSON(data); line, "After parse JSON" alert prompted correctly. 行,“ JSON解析后”警报正确提示。

I have check many document and reference but couldn't find out what is wrong with my $.parseJSON() . 我已经检查了许多文档和参考,但是找不到$.parseJSON()有什么问题。 Please advice, thank you. 请指教,谢谢。

//Show restaurant listing
$('#restaurantList').on("pagebeforecreate", function() {
    $.getJSON("http://mydomain/api/restaurant", function( data ) {
    alert('Hello');
    var rstList = $.parseJSON(data);
    alert('After parse JSON');

    });
});

Contrary to what the name implies, $.getJSON doesn't give you some JSON but the result of the parsing. 顾名思义, $.getJSON不会给您一些JSON,而是解析的结果。

From the documentation : 文档中

The success callback is passed the returned data, which is typically a JavaScript object or array as defined by the JSON structure and parsed using the $.parseJSON() method. 成功回调将传递返回的数据,该数据通常是由JSON结构定义并使用$ .parseJSON()方法进行解析的JavaScript对象或数组。

You data is already parsed, don't parse it. 您的data已经解析,请不要解析。

BTW, as Niet commented, you should have looked at the console to have a little more information on the error halting your script's execution. 顺便说一句,正如Niet所评论的那样,您应该查看控制台以获取更多有关停止脚本执行错误的信息。 See Using the console . 请参阅使用控制台

Because you are trying to parse a json object again. 因为您尝试再次解析json对象。 Which causes the error. 这会导致错误。 $.getJSON will return the json object. $ .getJSON将返回json对象。 You dont need to parse it again 您无需再次解析

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM