在 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?
P粉1762037812023-11-02 09:41:55
在您的 app/Http/Kernel.php
中检查 CORS 中间件是否存在:
protected $middleware = [ ... \Illuminate\Http\Middleware\HandleCors::class, ... ];
然后打开您的config/cors.php
。它的工作原理与 fruitcake/laravel-cors
完全相同:
['api/*', 'sanctum/csrf-cookie'], 'allowed_methods' => ['*'], 'allowed_origins' => ['*'], 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => false, ];