/**
* axios配置,输出为vue组建
* */
import axios from 'axios';
import qs from 'qs'
import CookiesJS from 'js-cookie'
import router from '../router'
// http request 拦截器
axios.interceptors.request.use(
config => {
config.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
// 在post请求发送出去之前,对其进行编码
if (config.method === 'post') {
config.data = qs.stringify(config.data);
}
return config;
},
err => {
return Promise.reject(err);
});
// http response 拦截器
axios.interceptors.response.use(
response => {
return response;
},
error => {
if (error.response) {
console.log("请求错误");
console.log(error.response.status);
switch (error.response.status) {
case 401:
// store.dispatch('logout');
console.log("401");
break;
case 404:
router.push('/Error/Error404');
break;
case 500:
router.push('/Error/Error500');
}
}
return Promise.reject(error);// 返回接口返回的错误信息
});
export default axios;