问题描述
uniapp h5请求后台数据时,前台抛出下面异常:
Access to XMLHttpRequest at 'http://127.0.0.1:8182/api/user/queryUsers?pageNo=1& pageSize=10' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
原因:
前后台分离,不在同一个域名下,产生跨域的问题
解决方案:
提示:就是在manifest.json h5 配置请求代理。
manifest.json
"h5": {
"template": "template.h5.html",
"router": {
"mode": "history",
"base": ""
},
"sdkConfigs": {
"maps": {
"qqmap": {
"key": "TKUBZ-D24AF-GJ4JY-JDVM2-IBYKK-KEBCU"
}
}
},
"async": {
"timeout": 20000
},
"uniStatistics": {
"enable": true
},
"devServer": {
"disableHostCheck": true,
"proxy": {
"/api": {
"target": "http://127.0.0.1:8182", //数据请求的接口网址
"changeOrigin": true, //是否开启跨域
"pathRewrite": {
"^/api": "/api"
}
}
}
}
}
前端配置:
版本:luch-request 3.x
npm 方式安装