在 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.
我必须安装 fruitcake/laravel-cors ( https://www.positronx.io/how-to-enable-cors-in-laravel/ )。
在 Laravel 9 中我找到了资料:
"将 Laravel CORS 集成到框架中 Dries Vints 将 fruitcake/laravel-cors 包迁移到 Laravel 框架中: 主要原因是我们想要删除我们依赖的循环依赖,此外我们还需要删除骨架的另一个依赖。 代码的所有学分归于@fruitcake 的@barryvdh。感谢您维护该软件包这么久!”。
我如何在新的 Laravel 中为 url: api/list 和 api/profiles 创建 cors?
最佳答案
在您的 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,
];
关于php - Laravel CORS 进入框架 v 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72939394/