Home » Php » php – Laravel cors work on local server but not in production

php – Laravel cors work on local server but not in production

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’ve been building app which has Laravel on backend and VueJS on frontend. In backend i installed fruitcake/laravel-cors for dealing with CORS (I also use tymon/jwt-auth for api auth). Everything is ok on local server but when i moved my app to production i still got error: Access to XMLHttpRequest at 'http://api.xxxxx.com/api/' from origin 'http://xxxxx.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.. I tried other packages from github and none of these solved problem.

Kernel.php:

protected $middleware = [
    \Fruitcake\Cors\HandleCors::class,
    ...
];
protected $middlewareGroups = [
    ...
    'api' => [
        'cors',
        ...
    ],
];
protected $routeMiddleware = [
    'cors' => \Fruitcake\Cors\HandleCors::class,
];

app.php:

'providers' => [
    ...
    \Fruitcake\Cors\CorsServiceProvider::class,
    ...
],

cors.php:

return [
    'paths' => ['*'],
    'allowed_methods' => ['*'],
    'allowed_origins' => ['*'],
    'allowed_origins_patterns' => [],
    'allowed_headers' => ['*'],
    'exposed_headers' => false,
    'max_age' => false,
    'supports_credentials' => false,
];
How to&Answers: