Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 无法从axios请求中捕获并记录错误响应大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的反应应用程序中有一个axios请求,我正在关注 axios npm docs.

这是我的axios请求

axios.post(Helper.getLoginApi(),data)
        .then((responsE) => {
            console.log(responsE);

            this.props.history.push(from.pathName)
        })
        .catch((error)=> {
            console.log(error);
        })

我能够成功地在成功的请求上记录数据.但是,当我故意产生错误并尝试console.log它时,我没有得到结果记录,我只是看到了

但是,当我在Chrome控制台中转到“网络标签”时,我会看到以下回复.

node.js – 无法从axios请求中捕获并记录错误响应

提前感谢您的帮助.

解决方法

Github Docs. axios请求的响应看起来像

{
  // `data` is the response that was provided by the server
  data: {},// `status` is the http status code from the server response
  status: 200,// `statusText` is the http status message from the server response
  statusText: 'OK',// `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},// `config` is the config that was provided to `axios` for the request
  config: {},// `request` is the request that generated this response
  // it is the last Clientrequest instance in node.js (in redirects)
  // and an XMLhttprequest instance the browser
  request: {}
}

所以基本上catch(error =>)实际上只是catch(response =>)

@R_450_10229@以记录error.response.data,你应该能够看到你的回复消息.

根据相同文档中的错误处理部分,您可以捕获错误响应

axios.post(Helper.getLoginApi(),data)
        .then((responsE) => {
            console.log(responsE);

            this.props.history.push(from.pathName)
        })
        .catch((error)=> {
            if (error.responsE) {
              // The request was made and the server responded with a status code
              // that falls out of the range of 2xx
              console.log(error.response.data);
              console.log(error.response.status);
              console.log(error.response.headers);
            } else if (error.request) {
              // The request was made but no response was received
              // `error.request` is an instance of XMLhttprequest in the browser and an instance of
              // http.Clientrequest in node.js
              console.log(error.request);
            } else {
              // Something happened in setTing up the request that triggered an Error
              console.log('Error',error.messagE);
            }
        })

大佬总结

以上是大佬教程为你收集整理的node.js – 无法从axios请求中捕获并记录错误响应全部内容,希望文章能够帮你解决node.js – 无法从axios请求中捕获并记录错误响应所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。