leostereo's avatar

Set CORS to allow cross site request.

Hi guys. Im having problems with CORS on laravel. I created two dockers services. One for the front end (axios on vuejs). The other for laravel backend runing with sail script. Is there same quick way to allow CORS for all methods for all routes. Working with raw php I used to do:

header('Access-Control-Allow-Origin: *'); header('Content-Type: application/json');

but this does not work here.

I readed about cors file , so I did:

´´´ return [

/*
|--------------------------------------------------------------------------
| Cross-Origin Resource Sharing (CORS) Configuration
|--------------------------------------------------------------------------
|
*/

'paths' => ['api/*', 'sanctum/csrf-cookie', '/*'],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => [],

'max_age' => 0,

'supports_credentials' => false,

]; ´´´

but can not fire axios get request with single param. Any help would be wellcome. Thanks!!!

0 likes
2 replies
JeromeFitzpatrick's avatar
Level 8

Hi @leostereo

What exactly is the action you are performing and what error your are getting? If your routes are protected by auth, make sure you change the supports_credentials to true and also set axios.defaults.withCredentials to true.

The 'allowed_origins' => ['*'] config accepts any origin similar to header('Access-Control-Allow-Origin: *')

Please or to participate in this conversation.