介绍
这篇文章主要介绍了thinkphp中withCredentials如何跨域,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
跨域是什么这里就不细讲,这里主要是thinkphp5.1,说一下大概的解决思路引用>首先,因为前端是自己写的,在<代码> axios> 代码配置中,我设置了如下
withCredentials:真正的//跨域请求时发送饼干
//,创建一个axios const service =, axios.create ({ baseURL才能:URL ,, withCredentials才能:真的,,//,跨域请求时发送饼干 时间,超时:大敌;5000年,//request 超时 })在后端的配置中,配置的是
头(“Access-Control-Allow-Origin: *“);故而抛出了这样一个错误
Access 用XMLHttpRequest at & # 39; http://store.ink/admin/me?sid=lbn3mpacfb3k1mbehnk9qh8kf3& # 39;,得到origin & # 39; http://vue-admin-web.ink& # 39;, has been blocked by CORS 政策:,,value of 从而& # 39;Access-Control-Allow-Origin& # 39;, header 拷贝,response must not be 从而wildcard & # 39; * & # 39;, when 从而请求# 39;s credentials mode is & # 39;包括# 39;只,credentials mode of requests initiated by 从而XMLHttpRequest is controlled by 从而withCredentials 属性。意思大概为设置<代码> withCredentials 代码>为<代码> 代码>时,起源是不允许为<代码> 代码>的,起源必须设置为来源的地址
也就是http://a.com请求http://b.com的时候,http://a.com必须设置起源为http://b.com才能通过
<强>最后参阅配置如下强>
,origin 美元;=,$ _SERVER [& # 39; HTTP_ORIGIN& # 39;], ? ?, & # 39; * & # 39;; ,,,,,,,头(“Access-Control-Allow-Origin: origin"美元;); ,,,,,,,头(& # 39;Access-Control-Allow-Credentials:,真正的# 39;); ,,,,,,,头(& # 39;Access-Control-Allow-Headers:,授权,内容类型,,If-Match,, if - modified - since,,具有,,If-Unmodified-Since,, X-Requested-With& # 39;); ,,,,,,,头(& # 39;Access-Control-Allow-Methods:,,,,,,,,,删除# 39;); ,,,,,,,头(& # 39;Access-Control-Max-Age:, 1728000 & # 39;);当然,为*的时候也可以这样
头(“Access-Control-Allow-Origin: *“); ,,,,,,,头(& # 39;Access-Control-Allow-Headers:,授权,内容类型,,If-Match,, if - modified - since,,具有,,If-Unmodified-Since,, X-Requested-With& # 39;); ,,,,,,,头(& # 39;Access-Control-Allow-Methods:,,,,,,,,,删除# 39;); ,,,,,,,头(& # 39;Access-Control-Max-Age:, 1728000 & # 39;);首先定义个中间件<代码> php认为:中间件CrossDomain 代码>
& lt; php ? namespace 应用\ http \中间件; use 认为\反应; class CrossDomain { ,,,public function 处理(请求,美元,\ Closure 下一美元) ,,,{ ,,,,,,,origin 美元;=,$ _SERVER [& # 39; HTTP_ORIGIN& # 39;], ? ?, & # 39; * & # 39;; ,,,,,,,头(“Access-Control-Allow-Origin: origin"美元;); ,,,,,,,头(& # 39;Access-Control-Allow-Credentials:,真正的# 39;); ,,,,,,,头(& # 39;Access-Control-Allow-Headers:,授权,内容类型,,If-Match,, if - modified - since,,具有,,If-Unmodified-Since,, X-Requested-With& # 39;); ,,,,,,,头(& # 39;Access-Control-Allow-Methods:,,,,,,,,,删除# 39;); ,,,,,,,头(& # 39;Access-Control-Max-Age:, 1728000 & # 39;); ,,,,,,,return 下一美元($请求); ,,,} }在<代码>路由器。php代码>中
路线::集团(& # 39;& # 39;,,function () { ,,,.... ,,,这里写路由 ,,,.... null null null null null null null null null null null null null null nullthinkphp中withCredentials如何跨域