Laravel CORS middleware fails for post and resource request
Had the same problem for hours now. Tried different solutions (used the barryvdh/laravel-cors library, made my own CORS middleware, added headers in the index.php file) but nothing helped me out.
Now I'm using https://github.com/neomerx/cors-illuminate and it works.
One of the differences between the cors-illuminate library and the laravel-cors library is in the installation guide.At the cors-illuminate guide it explicitly says that you have to add the middleware line directly after
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
Maybe the laravel-cors library would work also, if you add the Middleware directly after the CheckForMaintenanceMode Middleware. Didn't try it out.
maybe you can use the following to temporarily get around it:
I added in the cors middleware in my controller construct like so:
public function __construct() { // Apply the jwt.auth middleware to all methods in this controller // except for the index method. $this->middleware('jwt.auth', ['except' => ['index']]); $this->middleware('cors'); }
in my kernel:
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, 'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class, 'cors'=> \Barryvdh\Cors\HandleCors::class];
this appears to be working for me. Also, check your laravel log and debug your controller. I had a typo in my controller and the http response in my client console was the cors error.