跨域請求插件

v1.0.1
版本
2022-11-05
版本更新時間
6119
安裝
6
star
簡介
跨域請求插件
安裝
composer require webman/cors
使用
安裝后自動生效
注意
跨域請求可能包含OPTIONS
請求,如果你的跨域接口設置了路由,請確保使用Route::any(..)
或者Route::add(['POST', 'OPTIONS'], ..)
設置,或者統一給所有OPTIONS請求設置跨域,例如路由里設置類似如下配置
// 給所有OPTIONS請求設置跨域
Route::options('[{path:.+}]', function (){
return response('');
});
注意
如果ajax請求自定義了header頭,需要在中間件里 Access-Control-Allow-Headers 字段加入這個自定義header頭,否則會報Request header field XXXX is not allowed by Access-Control-Allow-Headers in preflight response.注意
跨域頭不能重復設置,例如webman開啟了跨域,nginx就不能再設置跨域,否則跨域失敗