javascript - How to retrieve JSON variables from a AJAX response - Stack Overflow

When I tried to fetch the values from a JSON response, I stucked up. Here is my codeCode:$.ajax({url: &

When I tried to fetch the values from a JSON response, I stucked up. Here is my code

Code:

$.ajax({
    url: 'checkvotes.php',
    dataType: "json",
    success: function(data) {
     // want to fetch UP and DOWN variables from JSON here
     }
 });

AJAX Response from PHP

{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}

When I tried to fetch the values from a JSON response, I stucked up. Here is my code

Code:

$.ajax({
    url: 'checkvotes.php',
    dataType: "json",
    success: function(data) {
     // want to fetch UP and DOWN variables from JSON here
     }
 });

AJAX Response from PHP

{"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]}
Share Improve this question edited Jul 23, 2012 at 14:13 TheAlbear 5,5938 gold badges54 silver badges86 bronze badges asked Oct 7, 2010 at 12:41 RajasekarRajasekar 19k35 gold badges109 silver badges140 bronze badges 2
  • 1 alert(data.id); which says undefined – Rajasekar Commented Oct 7, 2010 at 12:45
  • The object doesn't have an id property, it only has a sample property (which has a length 1 array as its value which… – Quentin Commented Oct 7, 2010 at 12:50
Add a ment  | 

3 Answers 3

Reset to default 6
$.ajax({
    url: 'checkvotes.php',
    dataType: "json",
    success: function(data) {
       var up = data.sample[0].up;
       var down = data.sample[0].down;
    }
 });

Try data.sample[0].up and data.sample[0].down. If in doubt, use this JavaScript to emulate the call:

var data = {"sample":[{"id":"1","message":"my message","up":"200","down":"34"}]};

Run that in a debugger and examine data.

var up = data['sample'][0]['up'],
    down = data['sample'][0]['down']

just print a console.log(data) to inspect your json

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744796671a4594244.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信