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
You need to specify the property to get
by looping you are going through each object in the array.. so the first iteration will get you
Which you can access the property in the loop by using
data[i].Townand so on