zoukankan      html  css  js  c++  java
  • 如果axios请求失败,如何获取后端接口返回的状态码及错误信息

    这两天在工作中遇到一个问题,一个请求返回400错误,我需要向用户展示后端返回的错误信息,但是用普通的catch方法只能获取到浏览器返回的400错误提示,不能获取到后端返回的,后经查阅得出下面方法:
    axios.get('/user/12345')
      .catch(function (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);
        }
        console.log(error.config);
      });
     

    参考文档: https://segmentfault.com/q/1010000011190809/a-1020000011210688
  • 相关阅读:
    I/O模型
    同步异步与协程
    GIL(全局解释器锁)
    解决pycharm启动慢
    操作系统发展史
    TCP和UDP
    粘包问题
    网络编程
    异常
    常用函数汇总
  • 原文地址:https://www.cnblogs.com/qilj/p/10113087.html
Copyright © 2011-2022 走看看