Javascript提取返回200但没有数据

问题描述 投票:0回答:3

我要求以下拉列表显示世界上所有国家/地区。所以我发现了这个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方法。但给我输出enter image description here

这里没有任何数据。即使我得到了成功的回应。为什么会这样?这与cors错误有关吗?

javascript fetch-api
3个回答
4
投票

您可以使用如下:

let url = 'https://restcountries.eu/rest/v1/all';

fetch(url)
.then(res => res.json())
.then((data) => {
  console.log(data);
})
.catch(err => { throw err });

1
投票

这适合我

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
     });
    });
}

-1
投票

response.type'cors'可能意味着Cross Origin Request - 它阻止它 - 试图找到另一个api

© www.soinside.com 2019 - 2024. All rights reserved.