分享thinkphp withCredentials跨域问题解决思路

下面由thinkphp教程栏目给大家介绍thinkphp withcredentials 跨域问题解决思路,希望对需要的朋友有所帮助!

thinkphp

// 创建一个axios const service = axios.create({   baseURL: URL ,    withCredentials: true, // 跨域请求时发送Cookie   timeout: 5000 // request timeout })
header("Access-Control-Allow-Origin: *");
Access to XMLhttpRequest at 'http://store.ink/admin/me?sid=lbn3mpacfb3k1mbehnk9qh8kf3' from origin 'http://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.
 $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');

Route::group('', function (){     ....     这里写路由     .... })->middleware(['CrossDomain']);
.... 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 = thinkresponseJson::create([], $type, 200, []);     return $response; # response  // 在异常处理接管中,必须返回的是一个人response响应, 而不是 `throw new `抛出一个响应 } ...

以上就是分享

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享