CORS problem with Laravel and Angular

If you are building your front-end application in Angular 2/4/5 and your APIs are built on Laravel and located in different server then sure you will face Cross Origin Request Sharing (CORS) problem and your APIs will not return expected results.

I faced the same issue and according to some solutions I added some headers on index.php of Laravel but still I was not able to solve issue.

I tried making a middleware but It was not working all time.

I tried many combinations but sometimes it works and sometime does not.

After some research I figured out that in angular before some GET/PUT/POST/DELETE type HTTP request OPTION type request hit the URL.

 

I solved my problem by using these both solutions.

 

Added these lines on public/index.php file

Created a Cors middleware in app/Http/Middleware/Cors.php

Register this middleware in app/Http/Kernel.php

Add this middleware in all urls in your route files like

 

Please follow and like us:
20

Leave a Reply