【发布时间】:2019-10-08 20:37:40
【问题描述】:
我在前端使用 react。为什么当状态码是409或任何400状态码时,我无法读取快递发送的数据/消息?
这就是我试图阅读响应的方式
hSubmit = event => {
console.log(this.state.checklistName)
event.preventDefault();
post.AddChecklistNames(this.state.token,this.state.category,this.state.checklistName)
.then(res=> {
if(res.status===201){
// window.location.reload(true);
}
else if(res.status===400||res.status===409||res.status===401||res.status===404||res.status===403){
console.log(res)
window.alert(res.data.error)
}
this.setState({checklistName:""})
})
发什么快递
response.status(409).json({ error: 'there is conflict'})
我无法读取错误
【问题讨论】:
-
您为什么不尝试无条件地记录您收到的
response。您可能没有收到您在if条件中使用的任何状态代码。 -
我们确实试过了,但什么也没得到。
-
我建议您尝试
res => { console.log(res) }并在此处发布您的问题的回复。如果那是空白,那么您需要调试是否发出任何请求或服务器没有响应/解析您的请求。请参阅浏览器的network选项卡以查看您的队列 -
res => { console.log(res) } 我们尝试了这个,但它什么也没显示,并且在网络响应中 409 即将到来,并且在控制台“错误:请求失败,状态码 409”这个错误是来了
-
你的
post.AddChecklistNames是什么?你能添加代码吗