Java脚本获取返回200,但没有数据 [英] Java script fetch returns 200 but no data
问题描述
我需要在下拉菜单中显示世界上所有国家. 因此,我找到了这个api端点 END POINT链接.当我将此端点链接复制并粘贴到Web浏览器中时,我得到了包含所有数据的响应. (国家);
I have a requirement to display all the countries in the world in a drop down. So I found this api end point END POINT LINK. When I copy and paste this end point link in my web browser I got a response with all the data. (countries);
当我尝试将其嵌入到项目中时.
When I try to embed this in project.
getCountries() {
try {
fetch(`https://restcountries.eu/rest/v1/all`).then(data =>
console.log(data)
);
} catch (error) {
console.log("HERE ERROR COMES", error);
}
}
它确实转到了fetch方法的代码块.但是给我输出
It does go to then block of the fetch method. But gives me the output
这里没有什么叫做数据.即使我得到了成功的回应. 为什么会发生这种情况?这与cors错误有关吗?
There is nothing called data here. Even I get a success respond. Why could this happen? Is this something related to cors errors?
推荐答案
您可以按以下方式使用:
You can use as follow:
let url = 'https://restcountries.eu/rest/v1/all';
fetch(url)
.then(res => res.json())
.then((data) => {
console.log(data);
})
.catch(err => { throw err });
这篇关于Java脚本获取返回200,但没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!