gpt4 book ai didi

node.js - 为什么我在收到 409 响应时无法读取消息或状态代码?

转载 作者:太空宇宙 更新时间:2023-11-04 01:30:39 24 4
gpt4 key购买 nike

我在前端使用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:""})
})

发什么 express

response.status(409).json({ error: 'there is conflict'})

我无法读取错误

最佳答案

我猜 res 没有定义。试试这个:

.then(res=> {
if(res.status===201){
// window.location.reload(true);
})
.catch (err) {
console.log(err.response.data)
window.alert(err.response.data)
}

当返回错误状态代码时,响应被视为错误,因此您应该在 catch block 中检查 error.response.data

关于node.js - 为什么我在收到 409 响应时无法读取消息或状态代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56255422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com