【发布时间】:2019-06-08 07:57:27
【问题描述】:
我正在使用 Promise 记录来自 api 的响应。但是每次身体都以空值登录。很可能是由于异步调用
用于从 fetch 调用 api。
function getDetails(url){
return new Promise((resolve, reject) => {
fetch(url, {mode: 'no-cors'}).then(res => {
resolve(res);
}, error => {
console.log(err);
})
})
}
var u = "https://get.geojs.io/v1/ip/country.json";
getDetails(u).then(function(resp){
console.log(resp);
})
我期待 api 在控制台中的响应
【问题讨论】:
-
fetch()已经返回了一个Promise,因此new Promise(...)是不必要的 -
如果我只是在函数中执行 fetch 和 .then() 而不返回任何内容,那么 body 仍然是 null
-
fetch()返回一个Responseobject。 -
function getDetails(url){ fetch(url, {mode: 'no-cors'}).then(res => { console.log(res) ; },error => { console.日志(错误); }) }
-
Trying to use fetch and pass in mode: no-cors 的可能重复项。不要使用“no-cors”模式。
标签: javascript promise request fetch response