获取请求错误,而我可以看到在浏览器开发工具的良好结果



我需要使用JS从本地主机服务器获取JSON数据。

取回请求抛出错误:

Uncaught (in promise) SyntaxError: JSON。解析:JSON数据第1列第1行数据的意外结束

看起来答案是空的,但是当我打开浏览器开发工具时,我可以在网络部分看到JSON数据。

fetch(url, init)
.then(res => res.json()) //return the error
fetch(url, init)
.then(res => res.text()) //return an empty string

我在网络部分看到的结果格式很好,当我复制/粘贴它以手动使用JSON.parse()时,我得到了正确的结果。

你必须返回承诺,然后等待:

const method = async() =>{
return fetch(url, init)
.then(res => res.json())
.catch(err)=>{
//manage error here
}
}

method().then(res=>....)

更多信息请看这里:如何获取从fetch() promise返回的数据?

最新更新