在Laravel 9之前当我有错误时:
Access to XMLHttpRequest at 'http://localhost:8000/demo' from origin 'null'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is
present on the requested resource.我必须安装水果蛋糕/拉拉-cors( https://www.positronx.io/how-to-enable-cors-in-laravel/ )。
在Laravel 9,我发现了一些信息:
“将Laravel CORS集成到框架中,Dries Vint将水果蛋糕/ Laravel -cors包迁移到Laravel框架中:主要原因是,除了消除框架的另一个依赖项之外,我们还希望删除一个循环依赖关系。代码的所有学分都可以转到@barryvdh的@子蛋糕上。谢谢您将这个包维护了这么长时间!”
例如,如何在新的Laravel中为url: api/list和api/profile制作cors?
发布于 2022-07-11 13:40:22
在您的app/Http/Kernel.php检查中,是否存在CORS中间件:
protected $middleware = [
...
\Illuminate\Http\Middleware\HandleCors::class,
...
];然后打开你的config/cors.php。它的工作原理与fruitcake/laravel-cors完全相同
<?php
return [
/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
| Here you may configure your settings for cross-origin resource sharing
| or "CORS". This determines what cross-origin operations may execute
| in web browsers. You are free to adjust these settings as needed.
|
| To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
|
*/
'paths' => ['api/*', 'sanctum/csrf-cookie'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];https://stackoverflow.com/questions/72939394
复制相似问题