项目中遇到了这个问题,记录一下我用的2种方法:
方法一:用浏览器插件
Allow CORS: Access-Control-Allow-Origin
uni官方推荐的插件: uniapp.dcloud.net.cn/tutorial/CO…
可以离线安装
方法二:vue.config.js配置代理
在项目根目录新建vue.config.js文件:
module.exports = {
devServer: {
disableHostCheck: true,
proxy: {
"/api": {
target: "http://test.com/",
changeOrigin: true,
secure: false,
ws: false,
},
},
},
};
接口请求文件也要修改:
baseURL = `http://${serverIp}:${serverPort}/`;
// #ifdef H5
options.url = `${options.url}`;
// #endif
// #ifndef H5
options.url = `${baseURL}${options.url}`;
// #endif