如何解决从Bandsintown API提取数据
我需要一些帮助,以从Bandsintown API提取事件数据。我在Postman App中测试了HTTP GET请求,它正确返回了数据。
现在,我正在尝试将这些数据提取到JavaScript中,然后在我设计的网站上呈现它,但似乎有些事情不起作用。这是我的代码:
var eventRequest = new XMLHttpRequest();
eventRequest.open('GET','https://rest.bandsintown.com/artists/${artistname}/events?date=upcoming&app_id=${app_id}');
eventRequest.onload = function() {
var eventsData = eventRequest.responseText;
};
eventRequest.send();
要在我正在使用的JS中进一步渲染数据:
document.getElementById("app").innerHTML = `
${eventsData.map(eventTemplate).join("")}
`;
解决方法
您可以尝试此操作,并且在日志中可能会看到进一步的错误。
var eventRequest = new XMLHttpRequest();
eventRequest.open("GET","https://rest.bandsintown.com/artists/${artistname}/events?date=upcoming&app_id=${app_id}",true);
eventRequest.onreadystatechange = function (event) {
if (eventRequest.readyState === 4) {
if (eventRequest.status === 200) {
console.log(eventRequest.responseText)
} else {
console.log("Error",eventRequest.statusText);
}
}
};
eventRequest.send(null);
,
此外,我非常确定,如果您想在URL($ {artistname})中使用字符串文字,则需要用反引号(``)代替简单或双引号。
'https://rest.bandsintown.com/artists/${artistname}/events?date=upcoming&app_id=${app_id}'
到
`https://rest.bandsintown.com/artists/${artistname}/events?date=upcoming&app_id=${app_id}`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。