【发布时间】:2014-05-20 10:07:04
【问题描述】:
我目前正在使用 Spotify 元数据 API 实现专辑搜索自动完成功能。我已经完成了大部分功能,但是在进行嵌套调用以检索专辑封面时遇到了麻烦。我相信这是我问题的根源。当我执行 ajax 调用来检索图像时,它确实有效,并且我得到了正确的数据,但返回语句没有被执行。我要做的是获取前四个结果,每个结果都获取一个图像并返回标签、项目和图像。
$('#spotify-album-search').autocomplete({
source:
function (query, process) {
$.when(
$.ajax({
url: 'http://ws.spotify.com/search/1/album.json?q=' + query.term,
})
).then(function (data) {
process($.map(data.albums.slice(0, 4), function(item) {
$.when (
$.ajax({
url: 'https://embed.spotify.com/oembed/?url=' + item.href,
dataType: 'jsonp'
})
).then(function (image) {
// Input: The Rolling Stones
console.log(item.artists[0].name + ' - ' + item.name + ': ' + image.thumbnail_url);
// Console: The Rolling Stones - Let It Bleed: https://d3rt1990lpmkn.cloudfront.net/cover/91205a1c80960d7055f8ed1bbe022f195e1767a4
return { label: item.artists[0].name + ' - ' + item.name, album: item, image: image.thumbnail_url };
});
}));
});
},
select: function (e, ui) {
console.log("selected= " + ui.item.album);
},
messages: {
noResults: '',
results: function() {}
}
})
.data('ui-autocomplete')._renderItem = function(ul, item) {
return $('<li></li>')
.data( "ui-autocomplete-item", item)
.append('<a>' + item.label + '<img src="' + item.image + '" alt="" />' + '</a>')
.appendTo(ul);
};
编辑:
在这里你可以找到一个工作小提琴,如果你相信可以帮助! http://jsfiddle.net/9GbkL/
【问题讨论】:
-
第一个 Ajax 没有指定 JSONP。
-
我不认为这是问题所在!添加该行将中断请求。试试小提琴,你会看到的。谢谢!
标签: javascript jquery jquery-ui jquery-ui-autocomplete spotify