Setting response header with javascript

夙愿已清 提交于 2019-12-05 09:33:23

I researched a bit and found out that response headers have to be set to Access-Control-Allow-Origin: *

How can I do that using pure javascript? No jquery or any other library.

You can't, not unless your server is running JavaScript (NodeJS, etc.).

The server has to allow access to the resource from the origin of your document. The way it works is:

  • The browser asks permission to access the resource (this is called a "preflight" request), telling the server what resource it wants access to, etc.

  • The server replies with the appropriate headers telling the browser whether access will be allowed.

  • The browser sends the actual request.

  • The server responds to it (again including the relevant headers).

I believe there are situations where the pre-flight isn't necessary. All of that is handled for you by the XMLHttpRequest object.

Details in the Cross-Origin Resource Sharing specification.

You cannot do this on client side, your server must send these headers.

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