laravel 使用中间件解决跨域问题

xiaoxiao2021-02-28  24

创建中间件:

php artisan make:middleware CrossHttp

在中间件的中添加下列代码:这个是所有域都能访问

public function handle($request, Closure $next) { $response = $next($request); $response->header('Access-Control-Allow-Origin', '*'); $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept'); $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS'); $response->header('Access-Control-Allow-Credentials', 'false'); return $response; }

指定域名访问:

$response->header('Access-Control-Allow-Origin', 'http://mytest.com');

设置\app\Http\Kernel.php

全局访问:

指定域名访问:

转载请注明原文地址: https://www.6miu.com/read-2625293.html

最新回复(0)