如何处理Axios api服务文件中的错误?

时间:2019-07-15 14:47:32

标签: axios

我有一个Api服务,可使用Axios处理http请求。如何轻松处理此文件中的错误?我尝试了以下catch放置,但是在那儿不起作用。感谢您的帮助!

import axios from 'axios'
var Config = require('./config.json')

const apiClient = axios.create({
  baseURL: Config.api,
  //timeout: 3000,
  withCredentials: false, //This is the default
  headers: {
    Accept: 'application/json'
  }
})
.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);
  })

export default {
  getIssues() {
    return apiClient.get('/Issues')
  },
  getIssue(id) {
    return apiClient.get('/Issue/' + id)
  },
  postIssue(issue) {
    return apiClient.post('/Issue', issue)
  },
  updateIssue(issue) {
    return apiClient.put('/Issue', issue)
  },
....etc!....
}

0 个答案:

没有答案