新手来了 我编写了一个简单的测试函数,它遍历一堆歌曲列表,并为每个列表尝试从 Last.fm API 获取专辑封面图像。如果它不起作用,它应该在控制台中显示曲目和艺术家姓名。但是,当我运行它时,它总是显示相同的艺术家/曲目对,即最后一个迭代的,而不是触发错误的特定的。我不确定发生了什么事。
$(document).ready(function(){
array = []
for (var item in billboard) {
var track = billboard[item]['song'];
var artist = billboard[item]['artist'];
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=myAPIkey&artist=" + artist + "&track=" + track + "&format=json&callback=?", function(data) {
try{
image = data.track.album.image[2]['#text'];
}
catch (err) {
console.log(track + " - " artist);
}
});
}
});
var billboard = {
"7-12-1986": {"artist": "Simply Red", "song": "Holding Back The Years"},
"6-28-1986": {"artist": "Patti LaBelle ", "song": "On My Own"},
"7-5-1986": {"artist": "Billy Ocean", "song": "There'll Be Sad Songs (To Make You Cry)"},
.....
}