I have a AJAX call that returns some JSON like this:
$(document).ready(function () {
$.ajax({
type: 'GET',
url: 'http://example/functions.php',
data: { get_param: 'value' },
success: function (data) {
var names = data
$('#cand').html(data);
}
});
});
Inside the #cand
div I'll get:
[ { "id" : "1", "name" : "test1" },
{ "id" : "2", "name" : "test2" },
{ "id" : "3", "name" : "test3" },
{ "id" : "4", "name" : "test4" },
{ "id" : "5", "name" : "test5" } ]
How can I loop through this data and place each name in a div?
setting
dataType:'json'
will parse json for youor else you can use
parseJSON
here is how you can iterate
iterate using
each
http://jsfiddle.net/fyxZt/4/
Try following code, it works in my project:
Use that code.
ok i had the same problem and i fix it like this by removing
[]
from[{"key":"value"}]
:and also you can loop it if you want
Json data
When retrieve
I agree with all the above solutions, but to point out whats the root cause of this issue is, that major role player in all above code is this line of code:
when you miss this line (which is optional), the data returned from server is treated as full length string (which is default return type). Adding this line of code informs jQuery to convert the possible json string into json object.
Any jQuery ajax calls should specify this line, if expecting json data object.