前端跨域问题

2023-10-22 08:36:39 浏览数 (2)

如果后端接口没有设置 cors (跨域资源共享),可在前端解决跨域问题,在 vue.config.js 中配置代理:

代码语言:javascript复制
  devServer: {
    client: {
        overlay: false
    },
    historyApiFallback:true,
        allowedHosts:'all',
        port: 8080,
        proxy: {
        '/api': {
            // target:"http://172.29.133.140:8081/api",
            // target: 'http://127.0.0.1:8081',
            // target: 'http://172.40.236.81:8907',//网关 gqc
            target: 'http://172.29.133.135:8081/api',
                // target: 'http://172.29.133.137:8081/api',//陆强
                //target: 'http://172.29.132.54:8081/api',//吕世建
                // target: 'http://172.29.131.100:8081/api',//于勇
                //target: 'http://172.29.131.26:8081/api',//双双
                //  target:"http://172.29.131.26:8081",
                // target:"http://172.29.133.135:8080",
                pathRewrite: {
                '^/api': ''
            }
        }
    }
}

然后修改 .env 、 .env.development 文件中的下面这行代码,修改后需要重新运行项目:

代码语言:javascript复制
VUE_APP_API_BASE_URL=/api

0 人点赞