我试图从Last.fm API收集一些数据。使用邮差,并与下面的电话,我得到一个回应。
当我试图实现对Codepen相同的结果,我没有得到回应。这是我使用的代码:
fetch("http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&api_key=4a9f5581a9cdf20a699f540ac52a95c9")
.then(response => response.json())
.then(data => console.log(data))
我缺少的是在这里吗?
同时作为评论者提到
https://cors-anywhere.herokuapp.com/
(哈克变通)试试这个它应该工作:
fetch('https://cors-anywhere.herokuapp.com/http://ws.audioscrobbler.com/2.0/?method=geo.gettopartists&country=spain&format=json&api_key=4a9f5581a9cdf20a699f540ac52a95c9')
.then(response => response.json())
.then(data => console.log(data))
.catch(err => console.log(err))
注意:你不应该共享API密钥...