vue,uniapp解决h5跨域问题
如果有这样的跨域问题,解决办法:
✅ 第一步:在项目根目录下创建 vue.config.js
和 package.json 同级目录。
// vue.config.js
module.exports = {devServer: {proxy: {'/api': {target: 'https://app.yycjkb.cn', // 你的后端接口地址changeOrigin: true,pathRewrite: {'^/api': '/api' // 路径重写(可选)}}}}
}
✅ 第二步:改你的 request 文件,就是把你原先的后端路径的前面的域名去掉
this.config.baseUrl = 'https://app.yycjkb.cn/api'
改为
this.config.baseUrl = '/api'
然后掉接口的时候,比如this.get('/user/info')
实际访问的是 /api/user/info
被 vue.config.js 代理为 https://app.yycjkb.cn/api/user/info
✅ 第三步:重新启动项目即可