我要求以下拉列表显示世界上所有国家/地区。所以我发现了这个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);
}
}
这里没有任何数据。即使我得到了成功的回应。为什么会这样?这与cors错误有关吗?
您可以使用如下:
let url = 'https://restcountries.eu/rest/v1/all';
fetch(url)
.then(res => res.json())
.then((data) => {
console.log(data);
})
.catch(err => { throw err });
这适合我
function getCountries(){
fetch("https://api.printful.com/countries ")
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
let countries = data.result;
return countries.map(function(country){
console.log(country.name);
//Create your list here
});
});
}
response.type'cors'可能意味着Cross Origin Request - 它阻止它 - 试图找到另一个api