Home > Net >  Laravel: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response
Laravel: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response

Time:08-13

I have a Vue.js SPA being served by a Laravel backend. Both are separate projects. I am using fruitcake to handle CORS but I keep getting this error:

Access to XMLHttpRequest at 'http://domain.test/api/v1/attributes/3' from origin 'http://localhost:8081' has been blocked by CORS policy: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response. GET, POST methods work fine.

I have tried the solutions from similar problems here but none seem to work. I have even tried creating my own middleware like suggested here. Here is my config/cors.php that I am using with fruitcake.

'paths' => [],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,

I had assumed that 'allowed_methods' => ['*'], means all requests will be allowed. Anyone knows why?

CodePudding user response:

You have to configure at least one path, so that the middleware knows which routes to hook into:

'paths' => ['api/*']

CodePudding user response:

I'm use this code

public function handle($request, Closure $next)
{
    $response = $next($request);
    $response->header('Access-Control-Allow-Origin', '*');
    $response->header('Access-Control-Allow-Credentials', 'true');
    $response->header('Access-Control-Allow-Methods', 'GET,HEAD,OPTIONS,POST,PUT');
    $response->header('Access-Control-Allow-Headers', 'XMLHttpRequest, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Authorization, Access-Control-Request-Method, Access-Control-Request-Headers');
    
    return $response;
}
  • Related