Java 脚本获取 returns 200 但没有数据
Java script fetch returns 200 but no data
我需要在下拉列表中显示世界上所有国家/地区。
所以我找到了这个api终点END POINT LINK。当我在网络浏览器中复制并粘贴此端点 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 块。但是给我输出
这里没有所谓的数据。即使我得到成功的回应。
为什么会这样?这与 cors 错误有关吗?
responce.type 'cors' 可能意味着跨源请求 - 它正在阻止它 - 尝试找到另一个 api
您可以按如下方式使用:
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
});
});
}
我需要在下拉列表中显示世界上所有国家/地区。 所以我找到了这个api终点END POINT LINK。当我在网络浏览器中复制并粘贴此端点 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 块。但是给我输出
这里没有所谓的数据。即使我得到成功的回应。 为什么会这样?这与 cors 错误有关吗?
responce.type 'cors' 可能意味着跨源请求 - 它正在阻止它 - 尝试找到另一个 api
您可以按如下方式使用:
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
});
});
}