Jquery parse xhr.responseText

断了今生、忘了曾经 提交于 2019-12-12 02:57:47

问题


var data  = 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\"}"]

How do I get id, name. I tried like this data.id but I get this error

jquery JSON.parse: unexpected end of data.

Update

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.


回答1:


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.

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}
]


来源:https://stackoverflow.com/questions/15953325/jquery-parse-xhr-responsetext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!