Vue/cli代理axios请求跨域接口

vue.config.js文件配置

module.exports = {
    
    
    devServer: {
    
    
        // 设置代理
        proxy: {
    
    
            "/api": {
    
    
                target: "请求域名",
                ws: true,
                changOrigin: true,
                pathRewrite: {
    
    
                    "^/api": ""
                }
            }
        }
    }
};

axios配置的默认域名更改为

if (process.env.NODE_ENV === 'production') {
    
    
    axios.defaults.baseURL = '/' //生产环境需配置接口一级域名后的参数
} else {
    
    
    axios.defaults.baseURL = '/api/'
}

猜你喜欢

转载自blog.csdn.net/AK852369/article/details/113519101