Unexpected Access-Control-Allow-Origin header error

I've been trying to figure this out for a while and just can't get it!

I followed the tutorial ( https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps ), but instead of putting Angular inside / public folder, I created an external client.

Using MAMP PRO, I put the API at http: //www.jotbot.local and the client is at http: //www.jotclient.local . When I try to login, it gets this error:

XMLHttpRequest cannot load http://www.jotbot.local/api/authenticate. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.jotclient.local' is therefore not allowed access.

      

In CORS configuration in Laravel, I have:

return [ 
    'supportsCredentials' => false, 
    'allowedOrigins' => ['*'], 
    'allowedHeaders' => ['*'], 
    'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'], 
    'exposedHeaders' => ['*'], 
    'maxAge' => 0, 
    'hosts' => [], 
];

      

I'm not sure WHAT to change. Line 'allowedOrigins' => ['*'] or line 'hosts' => [], what needs to be changed?

Also, the client (in my real application) will be publicly available and will be passed to the client whenever it is processed, i.e. I won't know the domains I need to resolve, so I need SOME SUPPORT for the template, so I'm really confused. and the CORS package documentation is hard to find (or I don't know what I'm looking for ... LOL)

Thanks for any help you can provide.

+3


source to share


1 answer


I followed the same tutorial and faced the same problem.

You must add CORS for Laravel 5.1.

Follow the installation instructions and it will work fine.

https://github.com/barryvdh/laravel-cors

Require the barryvdh / laravel-cors package in your composer.json and update your dependencies.

$ composer require barryvdh/laravel-cors 0.7.x

      



Add Cors \ ServiceProvider to your config / app.php provider array:

 Barryvdh\Cors\ServiceProvider::class,

      

In routes.php file add middleware

Route::group(['prefix' => 'api','middleware' => 'cors'], function()

      

Or

 Route::group(['middleware' => 'cors'], function()

      

+2


source







All Articles