如何使用axios处理500条错误消息

时间:2019-06-13 09:10:34

标签: javascript axios

我无法使用axios在500 error中获得响应文本。

在开发工具的“网络”标签中,我可以找到这样的错误消息

{"Message":"500 error message 0","Code":0,"Type":"error"}

我用

axios()
.then(function(done) {
   //fine and can get done.data
})
.catch(function(error) {
    console.log(error.message);
}); 

但我只能得到

Request failed with status code 500

所以我如何使用axios获取响应文本

1 个答案:

答案 0 :(得分:0)

根据requestHeaders

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);
  });