Jquery parse xhr.responseText

2019-09-15 13:32发布

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条回答
劳资没心,怎么记你
2楼-- · 2019-09-15 14:14

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}
]
查看更多
登录 后发表回答