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

查看:45
本文介绍了使用 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');

附:您应该使用 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天全站免登陆