I wanted to know how to execute this method of the Lastfm api, it is simple, but I cannot execute it correctly. I know I'm accessing the label wrong
my attempt to use the method:
$.ajax({
type : 'POST',
url : 'http://ws.audioscrobbler.com/2.0/',
data : 'method=artist.gettopalbums&' +
'artist=cooldplay&' +
'api_key=57ee3318536b23ee81d6b27e36997cde&' +
'format=json',
dataType : 'jsonp',
success : function(data) {
$('#success #artistName').html(data.topalbums.artist.name);
},
error : function(code, message){
$('#error').html('Error Code: ' + code + ', Error Message: ' + message);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="success">
<div id="artistName"></div>
<div id="artistImage"></div>
<div id="artistBio"></div>
</div>
<div id="error"></div>
What would be the correct way to execute the .getTopAlbums method?
The error I'm getting in the chrome console is:
This happens because it
data.topalbums.artist
isundefined
and the reason for this is that itdata.topalbums
returns an object that contains an array calledalbum
and this just contains a list that you could access the first element to get the desired object that you can query theartist.name
Full code below: