如何解决ThinkPHP中使用withCredentials导致的跨域请求问题?
- 内容介绍
- 文章标签
- 相关推荐
本文共计732个文字,预计阅读时间需要3分钟。
由ThinkPHP教程栏目为大家介绍ThinkPHP withCredentials,解决跨域问题思路,希望对需要的朋友有所帮助!ThinkPHP withCredentials跨域问题解决思路,跨域是什么这里就不细讲,这里主要介绍thi...
下面由thinkphp教程栏目给大家介绍thinkphp withCredentials 跨域问题解决思路,希望对需要的朋友有所帮助!跨域是什么这里就不细讲, 这里主要是thinkphp5.1, 说一下大概的解决思路
首先,因为前端是自己写的, 在axios配置中, 我设置了如下
withCredentials: true // 跨域请求时发送cookie
// 创建一个axios const service = axios.create({ baseURL: URL , withCredentials: true, // 跨域请求时发送cookie timeout: 5000 // request timeout })
在后端的配置中,配置的是
header("Access-Control-Allow-Origin: *");
故而抛出了这样一个错误
Access to XMLHttpRequest at 'store.ink/admin/me?sid=lbn3mpacfb3k1mbehnk9qh8kf3' from origin 'vue-admin-web.ink' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
意思大概为 设置 withCredentials 为 true 时, origin 是不允许为 *的, origin必须设置为来源的地址
也就是 a.com 请求 b.com 的时候, a.com 必须设置origin 为 b.com 才能通过
最后参阅配置如下
$origin = $_SERVER['HTTP_ORIGIN'] ?? '*'; header("Access-Control-Allow-Origin: $origin"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000');
当然, 为 * 的时候也可以这样
header("Access-Control-Allow-Origin: *"); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000');
首先 定义个中间件 php think make:middleware CrossDomain
<?php namespace app\www.kancloud.cn/manual/thinkphp5_1/354092#_42
.... public function render(Exception $e) { # 这里来处理跨域问题 $origin = $_SERVER['HTTP_ORIGIN'] ?? '*'; header("Access-Control-Allow-Origin: $origin"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000'); $type = request()->isAjax() ? 'json' : "html"; $response = \think\response\Json::create([], $type, 200, []); return $response; # response // 在异常处理接管中,必须返回的是一个人response响应, 而不是 `throw new `抛出一个响应 } ...
完成。
相关推荐:最新的10个thinkphp视频教程
以上就是分享thinkphp withCredentials跨域问题解决思路的详细内容,更多请关注自由互联其它相关文章!
本文共计732个文字,预计阅读时间需要3分钟。
由ThinkPHP教程栏目为大家介绍ThinkPHP withCredentials,解决跨域问题思路,希望对需要的朋友有所帮助!ThinkPHP withCredentials跨域问题解决思路,跨域是什么这里就不细讲,这里主要介绍thi...
下面由thinkphp教程栏目给大家介绍thinkphp withCredentials 跨域问题解决思路,希望对需要的朋友有所帮助!跨域是什么这里就不细讲, 这里主要是thinkphp5.1, 说一下大概的解决思路
首先,因为前端是自己写的, 在axios配置中, 我设置了如下
withCredentials: true // 跨域请求时发送cookie
// 创建一个axios const service = axios.create({ baseURL: URL , withCredentials: true, // 跨域请求时发送cookie timeout: 5000 // request timeout })
在后端的配置中,配置的是
header("Access-Control-Allow-Origin: *");
故而抛出了这样一个错误
Access to XMLHttpRequest at 'store.ink/admin/me?sid=lbn3mpacfb3k1mbehnk9qh8kf3' from origin 'vue-admin-web.ink' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
意思大概为 设置 withCredentials 为 true 时, origin 是不允许为 *的, origin必须设置为来源的地址
也就是 a.com 请求 b.com 的时候, a.com 必须设置origin 为 b.com 才能通过
最后参阅配置如下
$origin = $_SERVER['HTTP_ORIGIN'] ?? '*'; header("Access-Control-Allow-Origin: $origin"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000');
当然, 为 * 的时候也可以这样
header("Access-Control-Allow-Origin: *"); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000');
首先 定义个中间件 php think make:middleware CrossDomain
<?php namespace app\www.kancloud.cn/manual/thinkphp5_1/354092#_42
.... public function render(Exception $e) { # 这里来处理跨域问题 $origin = $_SERVER['HTTP_ORIGIN'] ?? '*'; header("Access-Control-Allow-Origin: $origin"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE'); header('Access-Control-Max-Age: 1728000'); $type = request()->isAjax() ? 'json' : "html"; $response = \think\response\Json::create([], $type, 200, []); return $response; # response // 在异常处理接管中,必须返回的是一个人response响应, 而不是 `throw new `抛出一个响应 } ...
完成。
相关推荐:最新的10个thinkphp视频教程
以上就是分享thinkphp withCredentials跨域问题解决思路的详细内容,更多请关注自由互联其它相关文章!

