Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to set custom headers on js <script> requests?

Is something like this possible?

<script src="http://myserver.com/some.js" my-custom-header="foo"></script>

Update (a bit more detail):

I've been asked if there was a way to communicate some parameters to the server as part of the script request using headers instead of GET params. I said, "no," but thought I'd double check.

like image 228
sprugman Avatar asked Feb 01 '11 18:02

sprugman


People also ask

Can I add custom header to HTTP request?

In the Home pane, double-click HTTP Response Headers. In the HTTP Response Headers pane, click Add... in the Actions pane. In the Add Custom HTTP Response Header dialog box, set the name and value for your custom header, and then click OK.

How do I send a custom header with requests?

Custom Headers are for troubleshooting, informational purposes, and specific server-side logic. For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host.


1 Answers

Short answer: no. By default a script tag will just retrieve the resource specified in the src attribute.

However, if you use an AJAX request to retrieve the script (and add it later/execute it), you can use the setRequestHeader function of the XMLHttpRequest object (see http://www.developertutorials.com/learn-ajax/custom-http-headers-2643.php).

You could also use more complex methods, such as using mod_rewrite to rewrite paths, and include the parameters in the url. The best solution depends on what you want to do, and how much control you have over the server.

like image 105
dutchflyboy Avatar answered Oct 27 '22 23:10

dutchflyboy