UniApp-H5跨域

uni-appmanifest.json->h5->devServer配置:

微信截图_20210823095017.png

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"h5": {
"devServer": {
"port": 8080, // 端口
"disableHostCheck": true,
"proxy": {
"/apis": {
"target": "https://api.mokahr.com",
"changeOrigin": true, //是否跨域
"secure": false // 设置支持https
,"pathRewrite": {
"^/apis": ""
}
}
},
"https": true
}
}

请求页面:

微信截图_20210823095129.png

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
uni.request({ //数据请求
url: '/apis/api-platform/v1/data/ehrApplications',
method: 'GET',

data: {
stage: 'pending_checkin',
phone: that.formData.mailphone
},

header: {
'content-type': 'application/json',
'cache-control': 'no-cache'
},
success: res => {
if (res.statusCode == '500') {
uni.showToast({
icon: 'none',
title: res.data.errorMessage,
});
//this.resetForm();
return;
}
},
fail: (res) => {
uni.showToast({
icon: 'none',
title: res,
});
},
complete: () => {
//uni.hideLoading(); // 关闭loading
}
});