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

纵然是瞬间 提交于 2019-12-02 22:57:47

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", "*");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!