我需要在下拉列表中显示世界上所有国家/地区。 所以我找到了这个api端点END POINT LINK .当我在网络浏览器中复制并粘贴此终点链接时,我收到了包含所有数据的响应。 (国家);
当我尝试将其嵌入到项目中时。
getCountries() {
try {
fetch(`https://restcountries.eu/rest/v1/all`).then(data =>
console.log(data)
);
} catch (error) {
console.log("HERE ERROR COMES", error);
}
}
它会转到 fetch 方法的 then block 。但是给我输出
这里没有所谓的数据。即使我得到成功的回应。 为什么会这样?这与 cors 错误有关吗?
最佳答案
您可以按如下方式使用:
let url = 'https://restcountries.eu/rest/v1/all';
fetch(url)
.then(res => res.json())
.then((data) => {
console.log(data);
})
.catch(err => { throw err });
关于javascript - Javascript fetch 返回 200 但没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52312991/