I am trying to create a simple little jquery function that pulls JSON data from the url, then goes through each of the objects. All I want to do is to console.log the object.name so that I know that it is working. So far it is not working :( I checked the network tab in Chrome's Developer console and in the response it shows the json data was returned. The problem is I just can't get the object.name to console.log. If someone could help point out the problem I'd be greatful.
Here is the script:
function getJSON(){ $.ajax({ type: 'GET', url: '/api/data.json', success: function(data){ $.each(data, function(index, object){ console.log(object.name); }); } }); } getJSON(); Here is the JSON data:
[ { id: 1, name: "Greg", lastname: "Sugaro", school: "Georgia Tech" }, { id: 2, name: "Mike", lastname: "Wilder", school: "UGA" } ]
console.log(data)before the each call and see whether that is getting called... also check your browser console to see whether any error is getting loggedcomplete: function(xhr, status){ console.log(status); }to your ajax call after thesuccessone.JSON? Shouldid,name,lastname,schoolbe surrounded by double-quotes ? ;"id","name","lastname","school"? See stackoverflow.com/questions/8294088/javascript-object-vs-json , benalman.com/news/2010/03/theres-no-such-thing-as-a-json