使用 JavaScript 添加自定义 HTTP 标头

发布于 2024-07-14 06:21:07 字数 218 浏览 6 评论 0原文

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

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

此要求仅适用于 IE6 或 7。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

想挽留 2024-07-21 06:21:07

如果您使用 XHR,则 setRequestHeader 应该可以工作,例如

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

PS 您应该使用 Hijax 来修改锚点的行为,以便在由于某种原因 AJAX 无法为您的客户端工作时(例如页面上其他地方的脚本被破坏),它可以工作。

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

满意归宿 2024-07-21 06:21:07

我认为实现它的最简单方法是使用查询字符串而不是 HTTP 标头。

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

生活了然无味 2024-07-21 06:21:07

从浏览器内部向请求添加标头的唯一方法是使用 XmlHttpRequest setRequestHeader 方法。

将其与“GET”请求一起使用将下载资源。 关键在于以预期的方式访问资源。 表面上,您应该能够允许 GET 响应在短时间内可缓存,因此导航到新 URL 或创建带有 src url 的 IMG 标记应使用先前“GET”的缓存响应。 然而,这很可能会失败,尤其是在 IE 中,就缓存而言,IE 本身可能是一个法则。

最终我同意 Mehrdad 的观点,使用查询字符串是最简单、最可靠的方法。

另一个奇怪的替代方案是使用 XHR 向 URL 发出请求,表明您访问资源的意图。 它可以使用会话 cookie 进行响应,该会话 cookie 将由后续对图像或链接的请求携带。

The only way to add headers to a request from inside a browser is use the XmlHttpRequest setRequestHeader method.

Using this with "GET" request will download the resource. The trick then is to access the resource in the intended way. Ostensibly you should be able to allow the GET response to be cacheable for a short period, hence navigation to a new URL or the creation of an IMG tag with a src url should use the cached response from the previous "GET". However that is quite likely to fail especially in IE which can be a bit of a law unto itself where the cache is concerned.

Ultimately I agree with Mehrdad, use of query string is easiest and most reliable method.

Another quirky alternative is use an XHR to make a request to a URL that indicates your intent to access a resource. It could respond with a session cookie which will be carried by the subsequent request for the image or link.

南街九尾狐 2024-07-21 06:21:07

到 2021 年,这可以通过 Service Workers 来实现。

首先,您必须在页面上注册一个 Service Worker。

// index.html
window.addEventListener('load', function () {
    navigator
        .serviceWorker
        .register('/service-worker.js');
});

在 Service Worker 中,通过调用 WindowOrWorkerGlobalScope.fetch() 方法。

// service-worker.js
self.addEventListener('fetch', function (event) {
    event.respondWith(async function () {
        let headers = new Headers()
        headers.append("X-Custom-Header", "Random value")
        return fetch(event.request, {headers: headers})
    }());
});

请注意,某些 Web 浏览器不会在开发人员控制台中显示修改后的请求。 在这种情况下,可以使用 tcpdump、Wireshark 等工具或在服务器日志中观察请求。

In 2021, this can be accomplished with Service Workers.

First, you have to register a Service Worker on your page.

// index.html
window.addEventListener('load', function () {
    navigator
        .serviceWorker
        .register('/service-worker.js');
});

In the Service Worker a request is captured, modified and forwarded by calling the WindowOrWorkerGlobalScope.fetch() method.

// service-worker.js
self.addEventListener('fetch', function (event) {
    event.respondWith(async function () {
        let headers = new Headers()
        headers.append("X-Custom-Header", "Random value")
        return fetch(event.request, {headers: headers})
    }());
});

Please note that some web browsers do not show modified requests in the developer console. In that case requests can be observed with tools like tcpdump, Wireshark or in a server's logs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文