jsonp loop through the data
I have a webservice returning jsonp format. here's the code:
$(document).ready(function(){
$.getJSON("http://api.tubeupdates.com/?method=get.status&lines=central,victoria&return=name&jsonp=?",
function (result){
$.each(result.items, function(item){
$('body').append(item.response.lines[0].name);
});
}
);
});
It works fine if I remove the loop but fails with the $.each loop开发者_开发技巧. Any idea of what am I doing wrong?
Thanks
Mauro
The response doesn't have an array on an items
property, it looks like this:
{"response":{"lines":[{"name":"Central"},{"name":"Victoria"}]}}
It looks like you want to iterate the response.lines
array, in which case you need to do this:
$.each(result.response.lines, function(i, item){
$('body').append(item.name);
});
You can test it out here.
精彩评论