简体   繁体   English

阅读JSON Ajax响应?

[英]Reading JSON Ajax response?

I have an ajax response from my server. 我的服务器收到ajax响应。 Example below: 下面的例子:

{"user_id":"93","status_message":"Cool Status","timestamp":"1305648702"}

I tried reading the response using: 我尝试使用以下方法读取响应:

var json = eval(response);
var userid = json.user_id;

The above does not seem to work though. 上面的似乎不起作用。 Any ideas. 有任何想法吗。

You should use the JSON parser that is built in to many browsers these days. 这些天,您应该使用许多浏览器内置的JSON解析器。 If it's not available, you can use the JSON2 library , which provides the same interface, as a fallback. 如果不可用,则可以使用JSON2库 ,该提供相同的接口作为后备。

var json = JSON.parse(response);
var userid = json.user_id;

尝试不带eval(response)然后执行以下操作:

var userid = response.user_id;

I think you want to do 我想你想做

var json = JSON.parse(response);
var userid = json.user_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM