当前位置: 首页 > Web前端 > vue.js

vue或uni-app跨域解决方案

时间:2023-03-31 19:46:04 vue.js

常见解决方案服务器端有两种解决方案服务端告诉浏览器:你允许我跨域具体如何告诉浏览器,请看://告诉浏览器,只允许http://bb.aaa.com:9000这个源请求服务器$response->header('Access-Control-Allow-Origin','http://bb.aaa.com:9000');//告诉浏览器,请求头中只允许这些内容$response->header('Access-Control-Allow-Headers','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');//告诉浏览器只暴露'Authorization,authenticated'一个字段$response->header('Access-Control-Expose-Headers','Authorization,authenticated');//告诉浏览器只允许GET、POST、PATCH、PUT、OPTIONS方法进行跨域请求$response->header('Access-Control-Allow-Methods','GET,POST,PATCH,PUT,OPTIONS');//pre-check$response->header('Access-Control-Max-Age',3600);将以上代码写入中间件:///app/Http/Middleware/Cors.phpheader('Access-Control-Allow-Origin','http://bb.aaa.com:9000');//告诉浏览器请求头中只允许这些内容$response->header('Access-Control-Allow-Headers','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');//告诉浏览器只允许暴露这两个'Authorization,authenticated'Field$response->header('Access-Control-Expose-Headers','Authorization,authenticated');//告诉浏览器只允许GET、POST、PATCH、PUT、OPTIONS方法跨域请求$response->header('Access-Control-Allow-Methods','GET,POST,PATCH,PUT,OPTIONS');//预检$response->header('Access-Control-Max-Age',3600);返回$响应;}}在路由上添加跨域中间件,告诉客户端:服务端允许跨域请求$api->group(['middleware'=>'cors','prefix'=>'doc'],function($api){$api->get('userinfo',\App\Http\Controllers\Api\UsersController::class.'@show');})客户端解决方案欺骗浏览器让你认为你没有有跨域(其实还是跨域,使用代理)在manifest.json中加入如下代码://manifest.json"devServer":{"port":9000,"disableHostCheck":true,"proxy":{"/api/doc":{"target":"http://www.baidu.com","changeOrigin":true,"secure":false},"/api2":{.....}},},参数说明'/api/doc'捕获API标志,如果API中有这个"/api/doc"字符串,则开始匹配代理,比如API请求"/api/doc/userinfo",将被代理到请求"http://www.baidu.com/api/doc"即:将匹配的"/api/doc"替换为"http://www.baidu.com"/api/doc"客户端浏览器最终请求链接surface是:“http://192.168.0.104:9000/api/doc/userinfo”,但实际上是代理到:“http://www.baidu.com/api/doc/userinfo”向server为目标代理的API地址,也就是需要跨域的API地址地址可以是域名,比如:http://www.baidu.com也可以是IP地址:http://127.0.0.1:9000如果是域名,需要额外加一个参数changeOrigin:true,否则代理将失败。pathRewrite路径重写,也就是说会修改最终请求的API路径。比如访问的API路径:/api/doc/userinfo,设置pathRewrite后:{'^/api':''},最终代理访问路径:"http://www.baidu.com/doc/userinfo",将"api"替换为空字符串。该参数的作用是在命名代理后访问时删除名称。changeOrigin参数允许目标参数为域名。securesecure:false,不检查安全问题。设置后可以在HTTPS上运行,其他证书无效的后端服务器参数配置也可以使用。查看文档https://webpack.docschina.org/configuration/dev-server/#devserver-proxy请求封装uni.docajax=function(url,data={},method="GET"){returnnewPromise((resolve,reject)=>{vartype='application/json'if(method=="GET"){if(data!=={}){vararr=[];for(varkeyindata){arr.push(`${key}=${data[key]}`);}url+=`?${arr.join("&")}`;}type='application/x-www-form-urlencoded'}varaccess_token=uni.getStorageSync('access_token')console.log('token:',access_token)varbaseUrl='/api/doc/'uni.request({url:baseUrl+url,method:'GET',data:数据,header:{'content-type':type,'Accept':'application/x..v1+json','Authorization':'Bearer'+access_token,},success:function(res){if(res.data){resolve(res.data)}else{console.log("请求失败",res)reject(res)}},fail:function(res){console.log("发起请求失败~")console.log(res)}})})}请求示例://获取用户信息uni.docajax("userinfo",{},'GET').then(res=>{this.nickname=res.nicknamethis.avatar=res.avatar});