Refused to set unsafe header "Origin" when using xmlHttpRequest of Google Chrome Refused to set unsafe header "Origin" when using xmlHttpRequest of Google Chrome ajax ajax

Refused to set unsafe header "Origin" when using xmlHttpRequest of Google Chrome


This is just a guess, as I use jquery for ajax requests, including CORS.

I think the browser is supposed to set the header, not you. If you were able to set the header, that would defeat the purpose of the security feature.

Try the request without setting those headers and see if the browser sets them for you.


In CORS the calling code doesn't have to do any special configuration. Everything should be handled by the browser. It's the server's job to decide if request should be allowed or not. So any time you are making a request which breaks SOP policy, the browser will try to make a CORS request for you (it will add Origin header automatically, and possibly make a preflight request if you are using some unsafe headers/methods/content types). If the server supports CORS it will respond properly and allow/disallow the request by providing CORS specific response headers like

Access-Control-Allow-Origin: *

Keep in mind that Chrome is very restrictive about 'localhost' host name. (At least it was when I was working with it). Instead use your computer name or assign it another alias in 'hosts' file. So for example don't access your site like:

http://localhost:port/myappname

Instead use:

http://mymachinename:port/myappname

or

http://mymachinealias:port/myappname

For more details please check specification.


Are you working cross-domain?

Try Brian S solution or try this:

instead of setting to localhost just pass anything... and see what happens.

receiveReq.setRequestHeader("Origin", "*");