无法访问返回的 json 对象中的属性
我从 ajax 请求获取响应并将 request.responseText 存储在名为 requestData 的变量中。 requestData 数据包含 php 使用 json_encode() 传递的 json 对象。
请参阅我在下面制作的几条日志。
requestData : {"status":"ok","to":"","html":"<option value=\"Huberdeau\">Huberdeau<\/option><option value=\"Bo\u00eeleau\">Bo\u00eeleau<\/option><option value=\"Br\u00e9beuf\">Br\u00e9beuf<\/option><option value=\"Saint-R\u00e9mi-d'Amherst\">Saint-R\u00e9mi-d'Amherst<\/option><option value=\"Harrington\">Harrington<\/option>","message":"old"}
我的问题是我无法访问变量状态。以下调用均未正确获取该值。
requestData[status] : undefined
requestData.status : undefined
requestData['status'] : undefined
requestData[0]['status'] : undefined
当我尝试通过 for log 转储变量时,它会将其视为字符串。
dump_var :
0:{
1:"
2:s
3:t
4:a
5:t
6:u
7:s
8:"
9::
I get a response from an ajax request and store request.responseText in a variable called requestData. requestData data contains a json object pass by php using json_encode().
See a couple of log I made bellow.
requestData : {"status":"ok","to":"","html":"<option value=\"Huberdeau\">Huberdeau<\/option><option value=\"Bo\u00eeleau\">Bo\u00eeleau<\/option><option value=\"Br\u00e9beuf\">Br\u00e9beuf<\/option><option value=\"Saint-R\u00e9mi-d'Amherst\">Saint-R\u00e9mi-d'Amherst<\/option><option value=\"Harrington\">Harrington<\/option>","message":"old"}
My probleme is that I can't access the variable status. None of the following calls get the value properly.
requestData[status] : undefined
requestData.status : undefined
requestData['status'] : undefined
requestData[0]['status'] : undefined
And when I tried to dump my variable by a for log it treated it like string.
dump_var :
0:{
1:"
2:s
3:t
4:a
5:t
6:u
7:s
8:"
9::
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 jQuery 中,
使用$.getJSON 而不是
$.get
,那么你不需要执行 eval !
in jQuery
instead of $.get
use $.getJSON
then you don't need to do eval !!
我在另一个问题的评论中发现了窍门。无论如何,我将其发布在这里,因为在 原始讨论。
I have found the trick in a comment on an other question. I post it here anyway cause it's a bit difficult to notice in the original discussion.