提取响应没有被处理
我正在创建一个看起来与此相似的获取请求:
fetch('/endpoint', {
method: "POST",
headers: {"Content-Type": "application/json; charset=UTF-8"},
body: JSON.stringify(data)
})
.then((response) =>{
if(response.ok){
console.log("Response was ok");
back();
}
else {
const result = response.json();
result.then((result) => alert(result.message))
}
})
.catch(err => console.log(err))
}
供参考,这是后背函数:
const back = () => {
navigate('/dashboard')
}
我希望用户在请求完成后将用户路由回到仪表板组件。当仅绑在按钮上时,后背函数正确执行,如果响应存在问题,则该问题确实显示为警报。但是,为什么包含控制台日志的块和back()在响应还可以时不执行?
I'm creating a fetch request, that looks similar to this:
fetch('/endpoint', {
method: "POST",
headers: {"Content-Type": "application/json; charset=UTF-8"},
body: JSON.stringify(data)
})
.then((response) =>{
if(response.ok){
console.log("Response was ok");
back();
}
else {
const result = response.json();
result.then((result) => alert(result.message))
}
})
.catch(err => console.log(err))
}
For reference, this is the back function:
const back = () => {
navigate('/dashboard')
}
I would like the user to be routed back to a dashboard component after the request is complete. The back function executed properly when just tied to a button, and if there is an issue with the response, the issue does display as an alert. But why does the block containing the console log and back() not execute when the response is ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到问题在后端。响应是返回状态,但没有jsonified对象
之后
:
I realized that the problem was in the back end. The response was returning a status, but no jsonified object:
Before
After