服务器端api加如下代码:
// 告诉浏览器,只允许 http://bb.aaa.com:9000 这个源请求服务器,可以为*
$request->header('Access-Control-Allow-Origin: *');
$request->header('Access-Control-Allow-Credentials:true');
// 告诉浏览器,请求头里只允许有这些内容
$request->header('Access-Control-Allow-Headers: Origin, Client-Token, Authorization, Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, X-Requested-By, If-Modified-Since, X-File-Name, X-File-Type, Cache-Control, Origin');
// 告诉浏览器,只允许GET, POST, PATCH, PUT, OPTIONS方法跨域请求,可以为 *
$request->header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, OPTIONS');
// 告诉浏览器,只允许暴露'Authorization, authenticated'这两个字段,可以为 *
$request->header("Access-Control-Expose-Headers: Authorization, authenticated");
// 预检
$request->header('Access-Control-Max-Age', 3600);
或者客户端前端uniapp配置文件加如下代码:
"h5" : {
"router" : {
"mode" : "hash",
"base" : "/h5/"
},
"title" : "多客圈子",
"devServer": {
"port": 8081,
"https": false,
"disableHostCheck": true,
"proxy": {
"/api": { //api是需要转发的请求(所有带有 /api 前缀的请求都会转发给 target 后的地址 (代理2)
"target": "http://jiaoyou.local", // 你的目标接口域名
"changeOrigin": true, // 开启代理
"secure": false, //
"pathRewrite": { //去除请求前缀,保证交给后台服务器的是正常请求地址(必须配置)
"^/api": ""
}
}
}
},
参考:
https://www.jb51.net/article/180961.htm
https://blog.csdn.net/lqk11/article/details/106681097/
https://www.php.cn/faq/572241.html
https://blog.csdn.net/weixin_51715424/article/details/129295559
uniapp编译报错:
Browserslist: caniuse-lite is outdated. Please run: npx update-browserslist-db@latest
https://www.cnblogs.com/zhang-hong/p/14770410.html
https://blog.csdn.net/qq_53810245/article/details/141680035
最终解决方案:重装HBuilderX
原文链接:https://www.hertzdance.com/blog/uniapp-cors,转载请注明出处。
评论0