使用JavaScript添加自定义HTTP标头

问题描述:

在HTML页面上,在单击图像(img)或锚点(a)标签的链接时,我想为GET请求添加自定义标头。这些链接通常用于下载动态内容。这些标头可以是SAML标头或自定义应用程序特定标头。

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.

是否可以通过JavaScript添加这些自定义标头?或者如果我通过XMLHttpRequest添加这些,我如何实现下载功能?

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

此要求仅适用于IE6或7。

This requirement is for IE6 or 7 only.

如果你在*上使用 XHR ,然后 setRequestHeader 应该有效,例如

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

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

P.S。您应该使用 Hijax 来修改锚点的行为,以便它如果由于某种原因,AJAX不能为您的客户工作(如页面上其他地方的破坏脚本)。

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).