loop through json array jquery
Solution 1:
You have to parse the string as JSON (data[0] == "["
is an indication that data
is actually a string, not an object):
data = $.parseJSON(data);
$.each(data, function(i, item) {
alert(item);
});
Solution 2:
you could also change from the .get()
method to the .getJSON()
method, jQuery will then parse the string returned as data
to a javascript object and/or array that you can then reference like any other javascript object/array.
using your code above, if you changed .get
to .getJSON
, you should get an alert of [object Object]
for each element in the array. If you changed the alert to alert(item.name)
you will get the names.
Solution 3:
I dont think youre returning json object from server. just a string.
you need the dataType of the return object to be json