When the error occurred before 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.
I had to install fruitcake/laravel-cors (https://www.positronx.io/how-to-enable-cors-in-laravel/).
In Laravel 9 I found the information:
"Integrate Laravel CORS into the framework Dries Vints migrated the Fruitcake/laravel-cors package into the Laravel framework: The main reason is that we want to eliminate the circular dependency we depend on, in addition to eliminating another dependency of the skeleton. All credit for the code goes to @barryvdh of @fruitcake. Thank you for your long-term maintenance of this package! ”.
How to create cors for url: api/list and api/profiles in new Laravel?
P粉1762037812023-11-02 09:41:55
Check if the CORS middleware exists in your app/Http/Kernel.php
:
protected $middleware = [ ... \Illuminate\Http\Middleware\HandleCors::class, ... ];
Then open your config/cors.php
. It works exactly like fruitcake/laravel-cors
:
['api/*', 'sanctum/csrf-cookie'], 'allowed_methods' => ['*'], 'allowed_origins' => ['*'], 'allowed_origins_patterns' => [], 'allowed_headers' => ['*'], 'exposed_headers' => [], 'max_age' => 0, 'supports_credentials' => false, ];