[Vuejs]-Access-Control-Allow-Origin is missing laravel-9 and vuejs3

0👍

Create new middleware called Cors, inside of it, put this code:

public function handle($request, Closure $next)
    {
        $headers = [
            'Access-Control-Allow-Origin' => "*",
            'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS',
            'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin',
        ];
        if ($request->getMethod() == "OPTIONS") {
            return \Response::make('OK', 200, $headers);
        }
        $response = $next($request);
        foreach ($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }

Go to App\Http\Kernel.php, in the protected $middleware array, add this:

\App\Http\Middleware\Cors::class,

and inside protected $routeMiddleware array, add this:

'cors' => \App\Http\Middleware\Cors::class,

In your Route file, add this:

Route::middleware(['cors'])->group(function(){
    //your route here
});

Run this command inside your laravel folder:

php artisan optimize:clear

Leave a comment