下面由thinkphp教程欄目給大家介紹thinkphp withcredentials 跨域問題解決思路,希望對需要的朋友有所幫助!
// 創建一個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
喜歡就支持一下吧
相關推薦