我需要在下拉菜单中显示世界上所有国家/地区。
所以我找到了这个api端点端点链接 https://restcountries.eu/rest/v1/all。当我在网络浏览器中复制并粘贴此端点链接时,我收到了包含所有数据的响应。 (国家);
当我尝试将其嵌入到项目中时。
getCountries() {
try {
fetch(`https://restcountries.eu/rest/v1/all`).then(data =>
console.log(data)
);
} catch (error) {
console.log("HERE ERROR COMES", error);
}
}
It does go to then block of the fetch method. But gives me the output
这里没有什么叫做数据。即使我也得到了成功的回应。
为什么会发生这种情况?这与 cors 错误有关吗?
您可以按如下方式使用:
let url = 'https://restcountries.eu/rest/v1/all';
fetch(url)
.then(res => res.json())
.then((data) => {
console.log(data);
})
.catch(err => { throw err });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)