【发布时间】:2017-07-06 22:19:47
【问题描述】:
当使用 fetch 在服务器中使用标头返回请求时
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))
【问题讨论】:
-
你能显示完整的代码吗?
-
可以去掉第49行的逗号
-
@Chris 在第 49 行删除逗号不起作用
-
您的 API 端点返回的不是有效的 JSON。
-
当
fetch响应成功时,你是否将result状态设置为给定的响应?