1

I'm using jQuery to display last.fm data on my website. In this case, I'm trying to show my last song on my last.fm profile, using the code below:

$.getJSON('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=joaoramos&api_key=a72cc897d849cfbff9a677871699e3f2&limit=1&format=json&callback=?', function(data) {

    track = data.recenttracks.track[0];

    var artist  = track.artist['#text'],
        song    = track.name,
        url     = track.url,
        src     = track.image[3]['#text'];

    if (src == '' || src == null || src == 'undefined') {
        var src = 'http://joao.pt/wp-content/themes/joaoramos/img/music.jpg';
    }

    $('#lastfm a').attr('href', url);
    $('#lastfm a').attr('title', 'Last song scrobbed on Last.fm: <em>'+song+'</em> by '+artist);
    $('#lastfm a img').attr('alt', song+' by '+artist);
    $('#lastfm a img').attr('src', src);

});

I'm probably not using the correct path in the JSON object (some times I get a Uncaught TypeError: Cannot read property 'artist' of undefined console error, but not always), because most of the times nothing is shown on the third of the three circles on my website (being the first one the last shot on Dribbble, the second the last shot on Flickr, and the last one the last song on last.fm). Any suggestions?

João
  • 635
  • 1
  • 10
  • 25
  • I'll reset it or get a new one after I get a few suggestions on how to fix this, otherwise people won't be able to test the request.. or not? I'm just an enthusiast, not much of a security connoisseur :( – João Dec 03 '12 at 09:52

1 Answers1

1

You are retrieveing jsonp, not json so you need to use $.ajax with the correct dataType setting. Try this:

$.ajax({
    url: 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=[USERNAME]&api_key=[APIKEY]&limit=1&format=json&callback=?', 
    dataType: 'jsonp',
    success: function(data) {
        // your current code here...    
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339