UniApp本地调试H5跨域(谷歌chrome浏览器,UniApp内置浏览器不存在此问题)的推荐方案:欺骗浏览器,让浏览器觉得你没有跨域(其实还是跨域了,用的是代理)
客户器端解决方案(亲测可用):
1、在manifest.json里添加如下跨域代码:"h5" : {
"devServer" : {
"port" : 2001,//你H5网址的端口,如:http://localhost:2001/#/pages/index/index
"disableHostCheck" : true,
"proxy" : {
"/api" : {
"target" : "http://localhost:2000",//你api接口的请求地址,可以是域名,也可以是本地api地址,也可以是ip地址
"changeOrigin" : true,
"secure" : false
}
},
"https" : false
}
}
2、在你的vue请求页面,如index.vue的代码如下:uni.request({
url: '/api/index/index',
method: 'GET',
success: (res) => {
console.log(res);
}
});
下面这些解释可以不看,照抄上面代码即可
参数说明
"proxy"里的