laravel-cors icon indicating copy to clipboard operation
laravel-cors copied to clipboard

Only some route return No 'Access-Control-Allow-Origin' header is present on the requested resource.

Open wilson-young opened this issue 5 years ago • 20 comments

I am getting error Access to XMLHttpRequest at 'https://api.com/something' from origin 'https://website.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

but there only happen for 2 routes (until now, I am still testing on it) other routes either get or post didn't return me CORS error

image image

My NGINX didn't put any origin or CORS related. I also installed latest fruitcake 2.0.1 I have checked my own code too in controllers or route there are no problems. I am sure because the same project is deployed at another server with fruitcake 1.0.5 is working fine till now.

wilson-young avatar Jul 11 '20 17:07 wilson-young

This is similar to how ours is configured too. https://github.com/fruitcake/laravel-cors/issues/477#issuecomment-662146566

Our routes that use GET or POST without setting authentication with a Bearer token in the header for the POST seem okay but the ones that send authentication with a Bearer token show this error.

davidquon avatar Jul 21 '20 23:07 davidquon

@davidquon I still havent solved this issue yet.

wilson-young avatar Jul 22 '20 11:07 wilson-young

getting the same, and it's literally driving me insane.

neacon-ivalkenburg avatar Jul 27 '20 09:07 neacon-ivalkenburg

In case it helps others https://github.com/fruitcake/laravel-cors/issues/477#issuecomment-664709886. I'm using the 2.0 version on the server.

davidquon avatar Jul 28 '20 00:07 davidquon

If you're using the Authorization header, you should set credentials to true.

barryvdh avatar Jul 28 '20 07:07 barryvdh

Thanks @barryvdh as https://github.com/fruitcake/laravel-cors/issues/478#issuecomment-664827785 worked for me. 👏 https://github.com/fruitcake/laravel-cors/issues/477#issuecomment-665166972

davidquon avatar Jul 28 '20 17:07 davidquon

Thank you @barryvdh that worked!

Also, for anyone googling, make sure that if you created a custom routes file that you have updated your 'paths' array in the cors.php file. I was scratching my head for a bit then realized.

tominal avatar Jul 28 '20 17:07 tominal

If you're using the Authorization header, you should set credentials to true.

i have try to change credentials to true, but its still not worked and i also dont think that is the problem in my issue because when other request with Authorization is worked

but i found out maybe it will be problem but i havent solved yet too it is the 2 route i got cors error https://api.com/something is calling another api https://api-service.com/something (our team made too. its different service api) using same version of laravel and cors setting will it causing problem, any possible solution to try?

wilson-young avatar Aug 02 '20 15:08 wilson-young

The same problem here using "laravel-cors", but...

I've tested using my own CORS middleware implementation (the one I'm using in Lumen 6, which is working fine) and I got the same, so this issue is probably related to Lumen and not with this package.

Any suggestions would be appreciated!

Thanks! N

nbulian avatar Aug 23 '20 23:08 nbulian

If you're using the Authorization header, you should set credentials to true.

i have try to change credentials to true, but its still not worked and i also dont think that is the problem in my issue because when other request with Authorization is worked

but i found out maybe it will be problem but i havent solved yet too it is the 2 route i got cors error https://api.com/something is calling another api https://api-service.com/something (our team made too. its different service api) using same version of laravel and cors setting will it causing problem, any possible solution to try?

Hi, have you still this problem? Thank you

WPandu avatar Oct 18 '20 03:10 WPandu

If you're using the Authorization header, you should set credentials to true.

i have try to change credentials to true, but its still not worked and i also dont think that is the problem in my issue because when other request with Authorization is worked but i found out maybe it will be problem but i havent solved yet too it is the 2 route i got cors error https://api.com/something is calling another api https://api-service.com/something (our team made too. its different service api) using same version of laravel and cors setting will it causing problem, any possible solution to try?

Hi, have you still this problem? Thank you

Yes, i haven't solved this issue yet.

wilson-young avatar Oct 18 '20 16:10 wilson-young

Please, verify if you don't have a query error. I had the same problem and the error was I wrote " Album::all()->paginate(10) " To find if it's really a cors error, return response from the route with any string like 'hello' before any operation.

public function index(Request $request) { return response()->json('hello'); ... }

limitless-brain avatar Jan 12 '21 22:01 limitless-brain

In my case, what caused the issue is that some model doesn't have migration and a database Table which caused some of the query I made within that route to fail, it's strange that Laravel didn't notify me or return any error. I fixed the issue by removing the query to those tables.

Tonykaynoni avatar Feb 06 '21 05:02 Tonykaynoni

I was adding custom headers in only some use cases of the endpoint, so was getting this error message:

Access to XMLHttpRequest at 'https://my-production-url' from origin 'http://localhost' has been blocked by CORS policy: Request header field myCustomHeaderProperty is not allowed by Access-Control-Allow-Headers in preflight response.

So when my endpoint received an OPTIONS request, it needed to respond with: 'Access-Control-Allow-Headers': 'Authorization, myCustomHeaderProperty' then the subsequent GET request using the customHeader stopped having the CORS error.

BeatriceThalo avatar Apr 09 '21 18:04 BeatriceThalo

Please, verify if you don't have a query error. I had the same problem and the error was I wrote " Album::all()->paginate(10) " To find if it's really a cors error, return response from the route with any string like 'hello' before any operation.

public function index(Request $request) { return response()->json('hello'); ... }

It is really strange, it happened to me. Been scratching my head overnight. Laravel did not return any error related to the cause, but the CORS just blows up even it isn't the real cause. No error log too.

Your comment just saved me, thank you! I tried finding the error elsewhere and I knew it the second I added this code: public function index(Request $request) { return response()->json('hello'); ... }

salasmark avatar Jul 14 '21 03:07 salasmark

The same issue happened to me. I know that my answer will be a bit weird but it solved the issue for me. I noticed that my API calls (using Axios) which returns CORS error has a trailing "/" in the action URL. So I simply removed the trailing slash and the error was fixed. This didn't work: axios.post("something/") This worked: axios.post("something")

kareem-torky avatar Nov 01 '21 00:11 kareem-torky

In my case, a certain model is returning Cors error in a browser's console, but 500 in postman. I don't know what causes the issue. I can't find the problem since this worked well on my local server

Mikeonii avatar Dec 03 '21 04:12 Mikeonii

I figured out other clues. Get and All is not working.

Mikeonii avatar Dec 03 '21 07:12 Mikeonii

The same issue happened to me. I know that my answer will be a bit weird but it solved the issue for me. I noticed that my API calls (using Axios) which returns CORS error has a trailing "/" in the action URL. So I simply removed the trailing slash and the error was fixed. This didn't work: axios.post("something/") This worked: axios.post("something")

Hoooly sh... This was my mistake. a Fu...ing trailing slash

FatihAraz avatar Mar 18 '22 10:03 FatihAraz

The same issue happened to me. I know that my answer will be a bit weird but it solved the issue for me. I noticed that my API calls (using Axios) which returns CORS error has a trailing "/" in the action URL. So I simply removed the trailing slash and the error was fixed. This didn't work: axios.post("something/") This worked: axios.post("something")

you save my day,thanks

placecodex avatar Apr 07 '22 00:04 placecodex