开发者

Reading JSON Ajax response?

开发者 https://www.devze.com 2023-03-07 20:24 出处:网络
I have an ajax response from my server. Example below: {\"user_id\":\"93\",\"status_message\":\"Cool Status\",\"timestamp\":\"1305648702\"}

I have an ajax response from my server. 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 s开发者_开发技巧eem to work though. Any ideas.


You should use the JSON parser that is built in to many browsers these days. If it's not available, you can use the JSON2 library, which provides the same interface, as a fallback.

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


Try it without the eval(response) and just do:

var userid = response.user_id;


I think you want to do

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

精彩评论

暂无评论...
验证码 换一张
取 消