Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting response header with javascript

I'm having troubles with collecting json values from a URL in my application. When I try to get them a error log is displayed in the console saying that origin is not allowed by access-control-allow-origin.

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.

This is my current code:

<script type="text/javascript">
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", "http://example.com/id=69", false );
    xmlHttp.send( null );
    console.log("JSON values from URL: ");
    console.log(xmlHttp.responseText);
</script>
like image 255
Marko Ćilimković Avatar asked Aug 01 '13 09:08

Marko Ćilimković


People also ask

Can we set response header?

You can set response headers, you can add response headers And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.

Can Javascript read response headers?

While you can't ready any headers of HTML response in JS, you can read Server-Timing header, and you can pass arbitrary key-value data through it.

What is Javascript response header?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.

How do you set a response header in HTML?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.


2 Answers

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.

like image 149
T.J. Crowder Avatar answered Sep 29 '22 08:09

T.J. Crowder


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

like image 24
mguimard Avatar answered Sep 29 '22 08:09

mguimard