最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

axios请求拦截器错误处理程序中的错误对象的形状是什么?

IT培训 admin 6浏览 0评论

axios请求拦截器错误处理程序中的错误对象的形状是什么?

技术说明:由于axios使用不同的节点和浏览器库/机制,这个问题只涉及Node.js使用[email protected]

我可以为axios库()设置以下拦截器:

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    //
    // I am asking about this error handler and this error object
    //
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

当触发请求拦截器的错误处理程序中描述的回调时,该错误对象的形状是什么?

附:我看到this code describing work with errors中有axios

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) {
      //
      //
      //  !!!! This is a request error handler !!!
      //
      //
      // 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);
  });

请求错误处理程序中的error在后面的代码中表示什么?

回答如下:

当触发请求拦截器的错误处理程序中描述的回调时,该错误对象的形状是什么?

当拦截器“拒绝”代码的这部分内容时,拦截器将触发错误处理程序(.catch子句):

axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error); // <---- HERE
  });

axios错误对象的形状是一个JSON对象,如handling error section of axios docs on github所述

  • 消息:错误消息文本。
  • 响应:响应对象(如果已收到),如上一节中所述。在内部响应中,您将拥有数据,状态和标头对象
  • request:在浏览器上运行时的实际XMLHttpRequest对象或node.js中的http.ClientRequest实例。
  • config:原始请求配置。

请求错误处理程序中的错误在后面的代码中表示什么?

这将是您的axios拦截器绕过请求的错误响应

axios请求拦截器错误处理程序中的错误对象的形状是什么?

技术说明:由于axios使用不同的节点和浏览器库/机制,这个问题只涉及Node.js使用[email protected]

我可以为axios库()设置以下拦截器:

// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    //
    // I am asking about this error handler and this error object
    //
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

当触发请求拦截器的错误处理程序中描述的回调时,该错误对象的形状是什么?

附:我看到this code describing work with errors中有axios

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) {
      //
      //
      //  !!!! This is a request error handler !!!
      //
      //
      // 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);
  });

请求错误处理程序中的error在后面的代码中表示什么?

回答如下:

当触发请求拦截器的错误处理程序中描述的回调时,该错误对象的形状是什么?

当拦截器“拒绝”代码的这部分内容时,拦截器将触发错误处理程序(.catch子句):

axios.interceptors.response.use(function (response) {
    // Do something with response data
    return response;
  }, function (error) {
    // Do something with response error
    return Promise.reject(error); // <---- HERE
  });

axios错误对象的形状是一个JSON对象,如handling error section of axios docs on github所述

  • 消息:错误消息文本。
  • 响应:响应对象(如果已收到),如上一节中所述。在内部响应中,您将拥有数据,状态和标头对象
  • request:在浏览器上运行时的实际XMLHttpRequest对象或node.js中的http.ClientRequest实例。
  • config:原始请求配置。

请求错误处理程序中的错误在后面的代码中表示什么?

这将是您的axios拦截器绕过请求的错误响应

发布评论

评论列表 (0)

  1. 暂无评论