0

I return a list of values on JSON from database via ajax :

[{"Month":"02-2017","total":"10","total_2":"36"},{"Month":"01-2017","total":"74","total_2":"51"},{"Month":"12-2016","total":"26","total_2":"47"},{"Month":"11-2016","total":"307800","total_2":"111"}]

And I expected to after using parse.JSON(obj), I could push the items to a javascript variable and select if necessary:

                    var values = [];

                    $.ajax({
                        url:'data.php?values=yes',
                        dataType: 'json',
                        success:function(data){

                           $.each(data, function(key, item) {
                                //console.log(item);
                                values.push(item);
                            });


                        }
                    });

Select like "values[index].Month" or something like that.

What would be a good way to do this?

Answer: This way worked pretty well:

 function handleData( responseData ) {

    // Do what you want with the data
    console.log(responseData);
}

$.ajax({
    url: "hi.php",
    ...
    success: function ( data, status, XHR ) {
        handleData(data);
    }
});
Igor O
  • 301
  • 1
  • 6
  • 26
  • You shouldn't need to use `JSON.parse`, jQuery does that for you. There's even `$.getJSON('data.php?values=yes')` – Phil Feb 20 '17 at 02:03
  • @apsillers I'm doing the looping with $.each and using push to add the values in another variable. When I show the variable using console.log it shows [], and if I try to select values[0].Month it seems invalid. – Igor O Feb 20 '17 at 02:06
  • @apsillers sorry, edited now. – Igor O Feb 20 '17 at 02:10

0 Answers0