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

标签 javascript axios

我无法使用 axios 获取 500 错误 中的响应文本。

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

{"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 获取响应文本

最佳答案

根据axios readme :

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

关于javascript - 如何使用axios处理500错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56577124/

相关文章:

javascript - Axios 拦截在第一次调用时不应用来自 localStorage 的 token

javascript - 我想从 h4 标签获取文本

javascript - 如何在初始 vue.js/vue-router 加载时加载所有服务器端数据?

javascript - 尝试循环 axios 请求并异步推送

javascript - 我的 Vue 对象不是响应式的。它可以在本地运行,但不能在托管服务器上运行

reactjs - 如何在拦截器中使用 Axios cancelToken?

javascript - 如何修复 localhost 中的 react cors 错误?

javascript - "this"函数中 对象中 函数中

php - MySQL VARCHAR 字段或 PHP substr

javascript - 在文本字段后插入节点(表单)