Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome saying No 'Access-Control-Allow-Origin' header, but the header is there

This issue is mentioned in stackoverflow a dozen times already, but I have a different issue.

Chrome first makes an "OPTIONS" call to get the headers. As you can see, the correct headers are there.

enter image description here

For some reason, Chrome doesn't notice the header and cancels the actual request in the same manner that it would if the header wasn't there at all.

enter image description here

The page actually makes three calls and curiously, one of them works.

enter image description here

So the question is, when the header really is there, why does Chrome not respect it? What could I do to debug it?

Update

I tried adding Access-Control-Allow-Methods so now the header response from the OPTIONS call includes these response headers:

Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods:POST, GET, OPTIONS
Access-Control-Allow-Origin:*

The result is the same.

Setting the headers on the server side

I set the headers on the serverside on every request (in Global.asax.cs Application_BeginRequest)

Response.Headers.Add("Access-Control-Allow-Origin", "*");
Response.Headers.Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");

After investigating with Fiddler

I looked through the raw request and response and found something surprising: The error is a simple HTTP 500 error from the application. Likely because the HTTP 500 error does not contain the right headers, Chrome doesn't show the returned error but instead shows the header related error.

Answer

So in conclusion, if Chrome gives says No 'Access-Control-Allow-Origin' header it might actually cover up a HTTP 500 error. This can be determined by checking request and response in Fiddler.

like image 374
Niels Brinch Avatar asked Oct 01 '14 18:10

Niels Brinch


People also ask

How do I fix CORS header Access-Control allow Origin missing?

If the server is under your control, add the origin of the requesting site to the set of domains permitted access by adding it to the Access-Control-Allow-Origin header's value. You can also configure a site to allow any site to access it by using the * wildcard. You should only use this for public APIs.

How do I fix the problem with CORS in Chrome?

i. Turn OFF the CORS plugin, reload the app, at this time you should still get the errors which are correct. ii. Turn it back ON, reload the app, if the APIs are successful, stop here, no need to proceed to iii.

How do you resolve no Access-Control allow Origin header is present on the requested resource?

So to fix, type the target and the origin equally: make you Ajax code request pages/services to http://www.wordicious.com not http://wordicious.com . (Maybe place the target URL relatively, like '/login. php' , without the domain).

How do I get to control allow origin in Chrome?

Simply activate the add-on and perform the request. CORS or Cross-Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs). Installing this add-on will allow you to unblock this feature.


1 Answers

If Chrome says No 'Access-Control-Allow-Origin' header it might actually cover up a HTTP 500 error. This can be determined by checking request and response in Fiddler.

like image 179
Niels Brinch Avatar answered Sep 28 '22 10:09

Niels Brinch