Javascript 没有从fetch返回响应
我试图从API获取数据,但它不返回任何值。我试着直接把apiUrl放在浏览器中,它在那里工作。即使是通过邮递员的get请求也会返回请求Javascript 没有从fetch返回响应,javascript,fetch,Javascript,Fetch,我试图从API获取数据,但它不返回任何值。我试着直接把apiUrl放在浏览器中,它在那里工作。即使是通过邮递员的get请求也会返回请求 fetch(apiUrl) .then((response) => { let data = JSON.parse(response) console.log(data) return data; }) 同样在Chrome调试器中,网络选项卡中也没有请求。我在前面使用了相同的代码来获得响应。
fetch(apiUrl)
.then((response) => {
let data = JSON.parse(response)
console.log(data)
return data;
})
同样在Chrome调试器中,网络选项卡中也没有请求。我在前面使用了相同的代码来获得响应。使用Fetch调用API给出了一个承诺,将其转换为JSON将返回另一个承诺,您需要再次“等待”。应该是这样的
fetch(URL)
.then(response=>response.json())
.then(json=>console.log(json))
试试这个你怎么称呼这个?请检查这个你检查浏览器控制台有没有错误?您是否尝试过
.then(response=>response.json())
而不是手动解析响应@VLAZ通过点击按钮调用它,@str在控制台中没有错误。并尝试了response.json和async/await,但没有得到相同的结果。json.parse(promise)使用意外的令牌response.json()
返回一个promise
fetch(apiUrl)
.then((response) => {
return response.json().then( res => {
let data = res;
console.log(data)
return data;
})
})