Reputation: 1656
I am returning the data below from a PHP script:
[{"Town":"Mancetter"},{"Town":"Manchester"},{"Town":"Mancot Royal"}]
I basically just want to loop through the results and display the town, here is my jQuery:
function search_town(){
var keyword = $('.town_s').val()
$.ajax({
type: "GET",
url: "class/ajax.php",
data: { "town_search" : keyword },
success: function(data){
var data = $.parseJSON(data);
for (var i = 0, l = data.length; i < l; i++) {
alert(i + ': ' + data[i]);
}
}
});
}
I'm not getting the results desired... what is alerted is 0:[object Object]
Any ideas where I may be going wrong?
Thanks
Upvotes: 1
Views: 377
Reputation: 33661
You need to specify the property to get
data[i].Town
by looping you are going through each object in the array.. so the first iteration will get you
{"Town":"Mancetter"}
Which you can access the property in the loop by using data[i].Town
and so on
Upvotes: 4