jQuery解析xhr.responseText [英] Jquery parse xhr.responseText
问题描述
var data = xhr.responseText;
当我输出此console.log(xhr.responseText)
时.以下是我的输出
When I output this console.log(xhr.responseText)
. Below is my output
["{id:1,name\":\"JOHN\",\"city\":\"null\"}"
,"{\"id\":2,\"name\":\"MICHEAL\,\"city\":\"null\"}"]
如何获取id
,name
.我尝试这样data.id
,但出现此错误
How do I get id
, name
. I tried like this data.id
but I get this error
jquery JSON.parse:数据意外结束.
jquery JSON.parse: unexpected end of data.
更新
我将代码点火器与数据映射器一起使用,因此我的数据映射器给出了json响应.你知道吗,我该怎么解决.
I am using code igniter with data mapper so my data mapper is giving that json response. Do you know, how I can resolve it.
推荐答案
您已经被告知注释中存在问题:服务器生成的JSON无效.您可能没有使用库对JSON进行编码,永远不要手动对其进行编码.
You've already been told what the problem is in the comments: the JSON generated by the server is invalid. You are probably not using a library to encode your JSON, don't ever encode it by hand.
您的JSON可能看起来应如下所示(打印精美时) http://jsfiddle.net/7FKWr/
Your JSON should probably look like the following (when pretty printed) http://jsfiddle.net/7FKWr/
[
{"id": 1, "name": "JOHN", "city": null},
{"id": 2, "name": "MICHEAL", "city": null}
]
这篇关于jQuery解析xhr.responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!