views:

43

answers:

3

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"}]}
A: 
var up = data['sample'][0]['up'],
    down = data['sample'][0]['down']

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

Fabrizio Calderan
+1  A: 

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.

Aaron Digulla
+5  A: 
$.ajax({
    url: 'checkvotes.php',
    dataType: "json",
    success: function(data) {
       var up = data.sample[0].up;
       var down = data.sample[0].down;
    }
 });
reko_t