No 'Access-Control-Allow-Origin' header is present on the requested resource

m19 posted 4 years ago

I've tried a dozen "solutions" to prevent the No 'Access-Control-Allow-Origin' header is present on the requested resource error. But nothing seems to work.

I have a route like this:

Route::get('test', function () {
    header('Access-Control-Allow-Origin: *');
    echo 1;

Then I go to, choose jQuery 1.11 and put in the following code:

$.get('https://mydomain/api/0.1/index.php/test', function (result) {console.log(result)});

Which results in:

XMLHttpRequest cannot load https://mydomainapi/0.1/index.php/test. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. 

Anyone knows what I'm doing wrong here?

wing-dev replied 4 years ago

HI, I normally add this on the app/filter.php file

	header('Access-Control-Allow-Origin: *');
	header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
	header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Request-With');
	header('Access-Control-Allow-Credentials: true');

ermand replied 4 years ago

Thaknks wing-dev it really saved me :)

mrmonsters replied 4 years ago

Thanks a lot wing-dev!!

lancebutler2 replied 4 years ago

Just dropping by to say thanks, also. I tried adding the headers via a 'cors' filter and applying the 'cors' filter both before and after route. It did not work. But this suggestion did.

Meesha replied 1 year ago

Hi, Create a Cors middleware and register it in the application's global HTTP middleware stack in kernel.php. Follow this for more.

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us