test
test

Reputation: 18198

jQuery - Getting certain XML not working

Let's say I have an XML like this: http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=b25b959554ed76058ac220b7b2e0a026&track=Just%20for%20Me&artist=Hinoi%20Team

I'm trying to get the name of the track and it's not working: I do this:

$.ajax({
        type: "GET",
        url: "http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=b25b959554ed76058ac220b7b2e0a026&track="+yourTrack+"&artist="+yourArtist,
        dataType: "xml",
        success: function(xml) {
          $(".loading").css("display", "none");
            $(xml).find('track').each(function(){
                var id = $(this).find('id').text();
                console.log("Success!");

                var song = $(this).find('track > name').text();
                var name = $(this).find('artist name').text();
                var album = $(this).find('album title').text();

                $("#more").append("\nSong: " + song);
                $("#more").append("\nArtist: " + name);
                $("#more").append("\nAlbum: " + album);

                $("#more").append("\n\nlast.fm ID: " + id);

            });
        }
    });

and the result is this:

Song: 
Artist: Hinoi Team
Album: Super Euro Party

last.fm ID: 43135480

Help?

Upvotes: 0

Views: 92

Answers (1)

Blender
Blender

Reputation: 298532

The selector in this line isn't right:

var song = $(this).find('track > name').text();

$(this) is a <track> element. You are looking for another <track> element inside of it, which isn't what you need. You are looking for the <name>, so use this instead:

var song = $('> name', this).text();

Upvotes: 1

Related Questions