我试图以一种方式调用BandsInTown api,它使用我的请求URL中的&callback= parameter
返回一个jsonp对象。我对如何与Jquery调用一起工作感到困惑。我的代码看起来像这样,但没有做任何事情:
var shows=[];
var locat = "Denver,CO";
var artists = ["Twenty One Pilots", "Atmosphere", "Vince Staples", "STRFKR", "Rainbow Kitten Surprise", "Mac Demarco", "Hippo Campus", "Drake", "John Legend", "Rihanna", "Flying Lotus", "MGMT", "Jason Derulo", "M83", "Bon Iver", "Childish Gambino"];
for (var i = 0; i < artists.length; i++) {
parseEvent(artists[i], locat, shows);
}
function parseEvent(artist, locat, showsObject) {
var url = 'http://api.bandsintown.com/artists/' + artist + '/events/search.json?api_version=2.0&app_id=music_matcher&location=' + locat + '&radius=150&callback=bandsInInfo';
$.ajax({
url: url,
dataType: 'jsonp',
jsonpCallback: 'bandsInInfo'
});
}
function bandsInInfo(data) {
alert("workingBandy");
alert(data);
var numEvents = Object.keys(data).length;
for (var j = 0; j < numEvents; j++) {
if (!(data[j].venue.latitude == null && data[j].venue.latitude == 'undefined')) {
var element =
{
"location": {"latitude": data[j].venue.latitude, "longitude": data[j].venue.longitude},
"artist": data[j].artists[0].name,
"venue_name": data[j].venue.place,
"date": data[j].datetime,
"ticket_url": data[j].ticket_url,
"ticket_status": data[j].ticket_status,
"title": data[j].title
};
showsObject.push(element);
}
}
}
或者如果没有Jquery有办法做到这一点?这是api文档。除了将响应发送到您指定的回调函数之外,对此的描述并不多。 https://www.bandsintown.com/api/requests#artists-events提前感谢您提供任何帮助或建议!!
答案 0 :(得分:1)
JSONP回调函数与您要调用的函数(bandsInInfo)不同。当您将dataType定义为&#39; jsonp&#39;时,jQuery将为您管理回调函数。所以,我会从查询字符串中删除最后一个参数。
您应该将成功和错误属性添加到$ .ajax选项中:
function parseEvent(artist, locat, showsObject) {
var url = 'http://api.bandsintown.com/artists/' + artist + '/events/search.json?api_version=2.0&app_id=music_matcher&location=' + locat + '&radius=150';
$.ajax({
url: url,
dataType: 'jsonp',
success: function(data){
bandsInInfo(data);
},
error: function(err){
console.log(err)
}
});
}