A
A
Artemmmm132021-02-10 15:41:30
Laravel
Artemmmm13, 2021-02-10 15:41:30

How to enable CORS support in Laravel?

I need the application to be able to accept cors requests, I don't need to use the api yet, but just return a response from the web group, but I still need to pass the session (withCredentials). I tried to use the fruitcake / laravel-cors package, it helped and I was able to process requests, but I could not transfer the session. I tried to do it through custom middleware:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class HanldeRequest
{
    public function handle($request, Closure $next)
    {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Credentials',true)
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

But I get an error:
request has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I make a request through axios:
export const instance = axios.create({
withCredentials: true,
    baseURL: "http://site.local",
    headers: { 
    	Accept: "application/json" ,
    	"Access-Control-Allow-Origin": 'http://localhost:3000',
    	"Access-Control-Allow-Credentials": true  
    },
});

How to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question