proxy代理不生效、vue config.js不生效解决方法

vue config.js 配置跨域不生效

axios默认请求接口就是localhost,所以这里需要更改 axios设置的默认请求设置

在 main.js 文件里,设置

axios.defaults.baseURL = '/api'

配置跨域

vue.config.js文件夹要和src在同级别下

module.exports = {
    configureWebpack: {
        devtool: 'source-map'
    },

    productionSourceMap: false, // 生产环境是否要生成 sourceMap
    publicPath: './', //  部署应用包时的基本 URL
    outputDir: 'dist', //   打包时输出的文件目录
    assetsDir: 'assets', //   放置静态文件夹目录
    devServer: {
        host: 'localhost',
        port: 8081, //开发环境运行时的端口
        https: false, //是否启用HTTPS协议
        open: true, //项目运行成功后是否直接打开浏览器
        hot: true, //是否开启热加载
        allowedHosts: 'all',
        proxy: { //服务器代理
            '/api': {
                target: "http://localhost:8080/", // 实际跨域请求的API地址
                secure: false, // https请求则使用true
                ws: true,
                changeOrigin: true, // 跨域
                // 请求地址重写  http://front-end/api/login ⇒ http://api-url/login
                pathRewrite: {
                    '^/api': '/',
                }
            },
        }, // dev环境下,webpack-dev-server 相关配置
    }
}

在这里面 /api 就相当于'http://localhost:8080/'

所以接下来接口需要添加的的url参数不需要再写接口的域名

如果是封装的请求这个地址也不要写

要是在不行就把请求头加上

    headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": "application/json;charset=utf-8"
    }

如果还没有办法就让后端解决

springboot解决方法

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }

要使vue.config.js中的proxy生效,需要正确配置devServer.proxy选项。根据引用和引用的内容,可以将vue.config.js配置为以下形式: ```javascript module.exports = { devServer: { proxy: { '/api': { target: 'http://localhost:8081', changeOrigin: true, pathRewrite: { '^/api': '' } } } } } ``` 这样配置后,当访问以`/api`开头的接口时,会将请求代理到`http://localhost:8081`上。请注意确保代理目标服务器已启动并能够正确响应请求。 同时,还需要在main.js文件中设置axios.defaults.baseURL为'/api',如引用所述,以确保axios在发送请求时自动添加/api前缀来实现跨域访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue cli3 配置proxy代理无效的解决](https://download.csdn.net/download/weixin_38622467/14828750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [proxy代理不生效vue config.js不生效解决方法](https://blog.csdn.net/weixin_47379721/article/details/128576797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值