当在服务器中使用获取请求时使用 header 返回
SyntaxError: Unexpected end of input
at index.js:50
at <anonymous>
第 50 行代码是 }).then(res => res.json())
可能出了什么问题?
此代码获取。
fetch(api-url, {
mode: 'no-cors',
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': ' application/json',
'X-API-SERVER': '85499f9f'
},
}).then(res => res.json())
.then(res => {
if (res.status === 200){
console.log("accepted");
}else {
console.log(res.error);
}
console.log(res.error)
}).catch(err => console.log(err))
最佳答案
由于您正在请求 API,因此您不想禁用 CORS。只需在 fetch
请求中删除 mode: 'no-cors'
就足以解决此问题,只要您的 API 服务器也发送正确的 header (访问控制允许来源
)。
关于reactjs - 使用 header 获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959749/