且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel的“访问控制允许来源"和“访问控制允许标题"

更新时间:2023-01-09 09:09:00

您可以创建一个新的中间件并将标头添加到响应中:

You can create a new middleware and add the headers to the response:

运行php artisan make:middleware ModifyHeadersMiddleware

打开文件 ModifyHeadersMiddleware 并修改handle()方法:

Open the file ModifyHeadersMiddleware and modify the handle() method:

public function handle( $request, Closure $next )
{
    $response = $next( $request );
    $response->header( 'Access-Control-Allow-Origin', '*' );
    $response->header( 'Access-Control-Allow-Headers', 'Origin, Content-Type' );

    return $response;
}

打开 app/Http/Kernel.php ,然后在protected $middleware数组中添加 ModifyHeadersMiddleware 类.

Open app/Http/Kernel.php and in the protected $middleware array add the ModifyHeadersMiddleware class.