使用JavaScript添加自定义HTTP标头 [英] Adding custom HTTP headers using JavaScript

查看:116
本文介绍了使用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).

这篇关于使用JavaScript添加自定义HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆