Mauro74
Mauro74

Reputation: 4826

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

Upvotes: 1

Views: 906

Answers (1)

Nick Craver
Nick Craver

Reputation: 630469

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.

Upvotes: 3

Related Questions