vue中axios跨域问题

main.js配置
import axios from 'axios'
axios.defaults.baseURL = 'http://www.zhizous.cn:81/test';//请求的域名
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';这样设置那就错啦[需要使用request拦截器]

/**http request 拦截器**/
axios.interceptors.request.use(
    req => {
        // const token = 
        req.headers = {
            'Content-Type': 'application/x-www-form-urlencoded;'
        }
        return config;
    },
    err => {
        return Promise.reject(err);
    }
);
/**http response 拦截器**/
axios.interceptors.response.use(
    res => {
        if(res.data.errCode == 2){
            //做一些错误处理,如跳转到登录页等
        }
        return res;
    },
    err => {
        return Promise.reject(err.response.data);
    }
);

猜你喜欢

转载自www.cnblogs.com/zhizou/p/11610745.html