Adding custom HTTP headers using JavaScript(使用 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 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 JavaScript 添加自定义 HTTP 标头
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
