一、构建CORS响应域
项目目录下运行:
php artisan make:middleware Cors
二、修改Cors.php
文件目录:\app\Http\Middleware\Cors.php
修改后内容如下:
<?php namespace App\Http\Middleware; use Closure; class Cors { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); $response->headers->set('Access-Control-Allow-Origin','*'); $response->headers->set('Access-Control-Allow-Headers','Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json,application/x-www-form-urlencoded,text/plain,Authorization,X-CSRF-TOKEN,X-Requested-With'); $response->headers->set('Access-Control-Allow-Methods','GET, POST, PATCH, PUT, OPTIONS'); $response->headers->set('Access-Control-Allow-Credentials','true'); return $response; return $next($request); } }
三、修改:Kernel.php
目录:/app/Http/Kernel.php
将 \App\Http\Middleware\Cors::class,添加到$middleware属性,如图:
-