Laravel跨域端解决方案

1、建立中间件Cors.php

命令:php artisan make:middleware Cors

在/app/Http/Middleware/ 目录下会出现一个Cors.php 文件。

2、在handle 方法中加入如下内容

$response = $next($request);

$response->header('Access-Control-Allow-Origin', '*');

$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json');

$response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');

$response->header('Access-Control-Allow-Credentials', 'false');

return $response;

3、在 Kernel.php文件中的$middleware中加入刚刚添加的中间件:\App\Http\Middleware\Cors::class,

protected $middleware = [

    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,

    \App\Http\Middleware\EncryptCookies::class,

    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

    \Illuminate\Session\Middleware\StartSession::class,

    \Illuminate\View\Middleware\ShareErrorsFromSession::class,

    \App\Http\Middleware\Cors::class,

    ];

你可能感兴趣的:(Laravel跨域端解决方案)