建立或现用公共中间件中handle方法改为如下代码
public function handle(Request $request, Closure $next)
{
/**
* 用于跨域调用
*/
if($request->isMethod('OPTIONS')){
$response = response('',200);
}else{
$response = $next($request);
}
if($referrer = $request->server('HTTP_REFERER') ?? $request->server('HTTP_ORIGIN')){
preg_match('/^(http[s]?:\/\/)?([^\/]+)/i', $referrer, $match);
$response->header('Access-Control-Allow-Origin',"{$match[0]}");
$response->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE');
$response->header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization, Cookies, Token,content-type');
$response->header('Access-Control-Allow-Credentials', 'true');
$response->header('Cache-Control', 'no-store');
}
return $response;
}
评论 (0)