Search code examples
jsonapiitunes

iTunes search API undefined first result


Hi I'm struggling for a long time with the itunes api album listing.

My issue is the thumbnail and album title in the first <li></li> always comes back as undefined.

The code is based on itunes artist search which works as intended, but my version for album listing always gives this glitch.

$(document).ready(function(){
  var searchTerm = '909253';
  $.getJSON( "http://itunes.apple.com/lookup?id=" + searchTerm + '&limit=30' + '&entity=album' + '&callback=?', function(data) {
    $.each(data.results, function() {
      $('<li></li>')
        .hide()
        .append('<img src="' + this.artworkUrl60 + '" />' ) 
        .append('<span><a href="http://itunes.apple.com/search?term=' 
          + this.artistName + '">' + 'Artist: ' + this.artistName
          + '</a> ' + '<br />Album Title: '  + this.collectionName + '</span>')
        .appendTo('#results')
        .fadeIn();
    });        
    $("#results").listview("refresh");
  });
});    

​ See http://jsfiddle.net/tris_wood/u2sYe/2/

I've seen similar posts with this issue wth the itunes api but no solutions that I could find.

Any help would be greatly appreciated.


Solution

  • This is because the first returned element is always the parent element, in this case the artist.

    If you request:

    http://itunes.apple.com/lookup?id=909253&entity=album
    

    You will get (as you can see in wrapperType):

    0) Artist information
    1) First album
    2) Second album
    

    Unfortunately your code is temporary offline at the moment and I can't check if my intuition is correct.