vue axios 2 차 패키지

import axios from 'axios';import QS from 'qs';
//환경의 전환//if(process.env.NODEENV === 'development') {// axios.defaults.baseURL = 'http://172.16.70.100:9999/qd';// } else if (process.env.NODE_ENV === 'production') {// axios.defaults.baseURL = 'https://www.production.com';// }
axios.defaults.timeout = 5000;// http request 차단기 axios.interceptors.request.use(config=>{
config.headers = {
  'Content-Type':'application/x-www-form-urlencoded'
}
return config;

}, error => {
return Promise.reject(error);

});
//응답 차단기 즉 이상 처리 axios.interceptors.response.use(response=>{
if (response.status === 200) {
  return Promise.resolve(response);
} else {
  return Promise.reject(response);
}

}, err => {
if (err && err.response) {
  switch (err.response.status) {
    case 400:
        console.log('    ')
      break;
    case 403:
      console.log('    ')
      break;
    case 404:
      console.log('    ,      ')
      break;
    case 500:
      console.log('      ')
      break;
    case 501:
      console.log('     ')
      break;
    case 502:
      console.log('    ')
      break;
    default:
      console.log(`    ${err.response.status}`)
  }
} else {
  console.log('        ')
}
return Promise.resolve(err.response)

})
export default {
get(url, params){
  return new Promise((resolve, reject) => {
      axios.get(url,{
        params:params
      }).then(res => {
        resolve(res.data);
      }).catch(err =>{
        reject(err.data);
      })
  })
},
post(url, params) {
  return new Promise((resolve, reject) => {
       axios.post(url, QS.stringify(params))
      .then(res => {
          resolve(res.data);
      })
      .catch(err =>{
          reject(err.data)
      })
  });
}

};

좋은 웹페이지 즐겨찾기