Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom HTTP headers using JavaScript

On an HTML page, while clicking the link of an Image ("img") or anchor ("a") tags, I would like to add custom headers for the GET request. These links are typically for downloading dynamic content. These headers could be SAML headers or custom application specific headers.

Is it possible to add these custom headers through JavaScript? Or if I add these through XMLHttpRequest, how can I achieve the download functionality?

This requirement is for IE6 or 7 only.

like image 608
Srinivas Avatar asked Feb 24 '09 11:02

Srinivas


2 Answers

If you're using XHR, then setRequestHeader should work, e.g.

xhr.setRequestHeader('custom-header', 'value'); 

P.S. You should use Hijax to modify the behavior of your anchors so that it works if for some reason the AJAX isn't working for your clients (like a busted script elsewhere on the page).

like image 166
Hank Gay Avatar answered Sep 23 '22 18:09

Hank Gay


I think the easiest way to accomplish it is to use querystring instead of HTTP headers.

like image 34
mmx Avatar answered Sep 23 '22 18:09

mmx