Skip to content Skip to sidebar Skip to footer

JSON To JQuery: What Am I Doing Wrong?

I'm fairly new to javascript and jquery development. I have a table of values I'm fetching in PHP and formatting as a JSON string, because JSON looks like a great way to get data i

Solution 1:

The variable people must be an array. Change the first line to

var people = [];

At the moment "people" is just undefined and the .push() method only works on arrays.


Solution 2:

If your PHP writes the output like this:

[
    {"id":"1","name":"Bob","haircolor":"brown"},
    {"id":"2","name":"Carol","haircolor":"Red"}
]

You need to assign that whole structure to people:

var people;
$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
    people = data;
});

// note that you can't access anything inside people here
// only when the json has been processed

Solution 3:


Solution 4:

try initializing people variable as an array

var people = []

Solution 5:

You can try this:

$.getJSON("php/getpeople.php", function(data){ //getpeople.php generates the JSON
  $.each(data, function(i, people){
     console.log(people);
  });
});    

Post a Comment for "JSON To JQuery: What Am I Doing Wrong?"