前端解决跨域CORS 设置代理

当请求地址出现CORS跨域问题时,解决办法:

1.增加个 /api ( 可以叫别的) 字符来作为判断 

 

然后在vue.config.js里设置代理(vue.config.js 是  vue3 根目录下自己建的配置文件, vue2有对应的配置文件)

 devServer.proxy 配置 

 '/api' : {
                target: "http://121.41.14.175:10521", //请求目的ip: 端口
                // ws: true,//表示是否要开通链接websoket请求
                changeOrigin: true,
                pathRewrite: {
                    '^/api':''//这里理解成用‘/api’代替target里面的地址,
                    //后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可
                }
            }

请求成功! 

猜你喜欢

转载自blog.csdn.net/weixin_44220845/article/details/123667022