ajax - JSON parsing in javascript, the data is always undefined -
this question has answer here:
i trying parse random json file online, can try link , @ yourself. no matter for, 'undefined' when accessing data. want sort of output json. example, how can list of names ('nm') in file? no matter do, gives me undefined.
$.ajax( { type: 'get', url: 'http://mysafeinfo.com/api/data?list=englishmonarchs&format=json', contenttype: "application/json; charset=utf-8", datatype: "json", async: false, success: function(r) { alert(r.example); } });
the data fetched api array of objects this.
[{ nm: "edward elder", cty: "gb", hse: "house of wessex", yrs: "899-925" }, { nm: "athelstan", cty: "gb", hse: "house of wessex", yrs: "925-940" }]
one of ways iterate on array of objects $.each
success: function(r) { $.each(r, function(index, value){ // iterating on each object console.log(value.nm); // <---- accessing nm of each object }); }
Comments
Post a Comment