【问题标题】:Why can't I read message or status code on receiving 409 in response?为什么我在收到 409 响应时无法读取消息或状态码?
【发布时间】: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 是什么?你能添加代码吗

标签: node.js reactjs express


【解决方案1】:
我猜

res 没有定义。试试这个:

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

当返回错误状态码时,响应被视为错误,所以你应该在catch块中检查error.response.data

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多