Rambling my thoughts and experience

Solve CORS issue in the Laravel API(a dirty way)

Posted by Shailesh Davara on March 03, 2015 | 0 Minute Read

I’ve been developing an API in my favourite framework Laravel. It went very well. When I tried to consume that API in an another application, I come to a very known issue of CORS. I should also tell you that I was consuming this API in an Angular application.

Many of you have experienced below error, when you try to consume API.

XMLHttpRequest cannot load [weburl] No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[API hosted domain url]' is therefore not allowed access.

To overcome above error, I come to a temporary solution in Laravel 4.2. Add below lines on the top of routes.php file and the error will go.

	header("Access-Control-Allow-Origin: *");
	header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

I know above solution is not efficient, but till we go into production, It works and solve our issue.