vue中axios跨域问题

Posted 风未起云先动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }
);

 

以上是关于vue中axios跨域问题的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VUE axios POST 发送跨域 cros 问题

vue axios跨域怎么解决

vue中axios跨域问题