[英]Why can I not parse json string using jQuery.parseJSON(json)?
I am trying to parse a response of a string which is a json string. 我正在尝试解析一个字符串的响应,该字符串是json字符串。 In another page of my web app following code is working fine. 在我的网络应用的另一页中,以下代码运行正常。 but its not working for my current page i am working with. 但它不适用于我正在使用的当前页面。 Following is the code: 以下是代码:
$.ajax({
type: 'POST',
url: 'http://mywebapp.com/sendnames',
data: {},
success: function(result) {
alert('result: '+result);
var obj = jQuery.parseJSON(result);
alert('obj: '+obj);
// doing rest of stuff
}
});
first alert comes and shows right result. 出现第一个警报并显示正确的结果。 result is: 结果是:
[
"Richard",
"Eric",
"John"
]
but second alert does not come. 但是没有第二次警报。 i checked it, its a valid json. 我检查了它,它是一个有效的json。 why can not i parse this json with jQuery.parseJSON(). 为什么我不能使用jQuery.parseJSON()解析此json。 Thanks in advance. 提前致谢。
Try to add return type: dataType : json 尝试添加返回类型:dataType:json
$.ajax({
type: 'POST',
url: 'http://mywebapp.com/sendnames',
data: {},
dataType:'json',
success: function(result) {
console.log('result: '+result);
// doing rest of stuff
}
});
"json": “ json”:
Evaluates the response as JSON and returns a JavaScript object. 将响应评估为JSON并返回一个JavaScript对象。 In jQuery 1.4 the JSON data is parsed in a strict manner; 在jQuery 1.4中,以严格的方式解析JSON数据。 any malformed JSON is rejected and a parse error is thrown. 任何格式错误的JSON都会被拒绝,并引发解析错误。 (See json.org for more information on proper JSON formatting.) "jsonp": Loads in a JSON block using JSONP. (有关正确的JSON格式的更多信息,请参见json.org。)“ jsonp”:使用JSONP加载JSON块。 Adds an extra "?callback=?" 添加一个额外的“?callback =?” to the end of your URL to specify the callback. URL的末尾以指定回调。 Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. 通过将查询字符串参数“ _ = [TIMESTAMP]”附加到URL来禁用缓存,除非将cache选项设置为true。 http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
Replace $.ajax
by $.getJSON
. 将$.ajax
替换$.ajax
$.getJSON
。 This is guaranteed to trigger $.parseJSON
internally, so result
will already be the desired JS object. 确保可以在内部触发$.parseJSON
,因此result
将已经是所需的JS对象。
$.getJSON({
type: 'POST',
url: 'http://mywebapp.com/sendnames',
data: {},
success: function(obj) {
alert('obj: '+obj);
// doing rest of stuff
}
});
Try with adding dataType:'text'
, it will return string as result. 尝试添加dataType:'text'
,它将返回字符串作为结果。 and your code will run as you expect. 并且您的代码将按预期运行。
See the accepted answer here 在此处查看已接受的答案
You're parsing an object. 您正在解析一个对象。 You parse strings, not objects; 您解析字符串,而不是对象; jQuery.parseJSON only takes strings. jQuery.parseJSON只接受字符串。
Because $.ajax
already parsed the data, result
is Javascript object not a string. 因为$.ajax
已经解析了数据, result
是Javascript对象而不是字符串。 parseJSON
expects a string parameter. parseJSON
需要一个字符串参数。
FROM DOCS (more on .ajax()
data types here ): FROM DOCS (有关.ajax()
数据类型的更多信息,请.ajax()
此处 ):
The json type parses the fetched data file as a JavaScript object and returns the constructed object as the result data. json类型将提取的数据文件解析为JavaScript对象,并将构造的对象作为结果数据返回。 To do so, it uses jQuery.parseJSON() when the browser supports it; 为此,当浏览器支持时,它使用jQuery.parseJSON()。 otherwise it uses a Function constructor 否则使用Function构造函数
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.