如何清除 AJAX GET 调用的 HTTP 标头?

发布于 2024-07-14 13:07:34 字数 960 浏览 4 评论 0原文

我开发了一个解决方案,它依赖 AJAX 调用来检索信息并每 10 秒更新一次客户端页面。 这工作正常,但考虑到从客户端传递到服务器并再次返回的标头的数量和长度,我担心代码的可扩展性。 我已经删除了服务器端的一些冗余标头,大部分与 ASP.NET 相关,现在我正尝试减少来自客户端的标头。

我公司使用的浏览器是IE(版本6,即将升级到7)。 这是我当前代码的近似值:

var xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');

xmlHTTP.onreadystatechange = function() {
    if ((xmlHTTP.readyState == 4) && (xmlHTTP.status == 200)) {
        myCallbackFunction(xmlHTTP);
    }
};

xmlHTTP.open('GET', 'myUrl.aspx');

try {
    xmlHTTP.setRequestHeader("User-Agent", ".");
    xmlHTTP.setRequestHeader("Accept", ".");
    xmlHTTP.setRequestHeader("Accept-Language", ".");
    xmlHTTP.setRequestHeader("Content-Type", ".");
} catch(e) {}

xmlHTTP.send();

虽然我已经读到可以清除对于其中一些标头,我还没有找到在 IE6 中有效的方法。 将它们设置为 null 会导致类型不匹配异常,因此我最终只是将它们替换为“。” 暂且。 是否有其他方法来清除它们或减少提交的 HTTP 标头的替代方法?

此外,似乎根本没有办法替换或缩短“Referrer”标头。

I have developed a solution that relies on an AJAX call to retrieve information and update the client page every 10 seconds. This is working fine, but I am concerned at the scalability of the code, given the number and length of headers being passed from client to server and back again. I have removed a number of redundant headers on the server side, mostly ASP.NET-related, and now I'm attempting to cut down on the headers coming from the client.

The browser used by my company is IE (version 6, to be upgraded to 7 soon). This is an approximation of my current code:

var xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');

xmlHTTP.onreadystatechange = function() {
    if ((xmlHTTP.readyState == 4) && (xmlHTTP.status == 200)) {
        myCallbackFunction(xmlHTTP);
    }
};

xmlHTTP.open('GET', 'myUrl.aspx');

try {
    xmlHTTP.setRequestHeader("User-Agent", ".");
    xmlHTTP.setRequestHeader("Accept", ".");
    xmlHTTP.setRequestHeader("Accept-Language", ".");
    xmlHTTP.setRequestHeader("Content-Type", ".");
} catch(e) {}

xmlHTTP.send();

Although I've read that it's possible to clear some of these headers, I haven't found a way of doing it that works in IE6. Setting them to null results in a Type Mismatch exception, so I've ended up just replacing them with '.' for the time being. Is there another way of clearing them or an alternative method of reducing the submitted HTTP headers?

Also, there seems to be no way of replacing or shortening the 'Referrer' header at all.

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

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

发布评论

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

评论(3

落墨 2024-07-21 13:07:35

根据 WD 规范

如果作为参数给出的 HTTP 标头已经是请求标头列表的一部分,则 setRequestHeader() 方法会附加一个值。

也就是说,您只能添加标头,而不能替换它们。

这并不完全符合当前浏览器的行为,但它可能是浏览器的发展方向,在这种情况下,从长远来看,这方面的任何努力都是浪费时间。 无论如何,当前浏览器设置标头的行为多种多样,通常不能依赖。

似乎根本没有办法替换或缩短“Referrer”标头。

这不会让我感到惊讶,因为有些人错误地使用“Referer”[原文如此]作为访问控制机制。

您可以尝试确保当前页面 URL 不会过长,但说实话,所有这些对我来说都是过早优化的味道。 无论您做什么,您的请求都会容纳在一个 IP 数据包中,因此不会出现明显的性能差异。

Mibbit(正如您链接的博客中提到的)尝试这些东西可能是值得的,因为 Mibbit 吸引了相当惊人的流量,但对于一个简单的公司范围的应用程序,我不认为跨浏览器和-代理测试负担:弄乱标头的最终用户效益比是值得的。

According to the WD spec

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers.

That is, you could only add headers, not replace them.

This doesn't completely match current browser behaviour, but it may be where browsers will be headed, in which case any efforts on this front are a waste of time in the long term. In any case current browser behaviour with setting headers is very varied and generally can't be relied upon.

There seems to be no way of replacing or shortening the 'Referrer' header at all.

That wouldn't surprise me, given that some people misguidedly use ‘Referer’ [sic] as an access-control mechanism.

You could try to make sure that the current page URL wasn't excessively long, but to be honest all this smells of premature optimisation to me. Whatever you do your request is going to fit within one IP packet, so there's not gonig to be a big visible performance difference.

It may be worthwhile for Mibbit (as mentioned on the blog you linked) to try this stuff, because Mibbit draws a quite staggering amount of traffic, but for a simple company-wide application I don't think the cross-browser-and-proxy-testing-burden:end-user-benefit ratio of messing with the headers is worth it.

°如果伤别离去 2024-07-21 13:07:35

IE 6 及更早版本使用从 MSXML.XMLHTTP 创建的 ActiveXObject(实际上源自 IXMLHTTPRequest),而 IE 7 和其他现代浏览器(例如 Mozilla)使用名为 XmlHttpRequest 的内部对象。 这可能就是为什么您不能将 MSXML 实现的请求标头设置为 null 但可以将内置对象的请求标头设置为 null 的原因。

因此,我不认为有任何方法可以集体清除所有标头。 您提供的 Mibbit 链接仅提供了将所有标头一一设置为 null 的功能。 对于正常情况,减少标头对于减少流量负载来说可能微不足道。

也就是说,我很想知道为什么您将请求标头设置为 "." 而不是空字符串 ""

IE 6 and older versions use the ActiveXObject created from MSXML.XMLHTTP (actually derived from IXMLHTTPRequest), whereas IE 7 and other modern browsers such as Mozilla use an intrinsic object called XmlHttpRequest. This is probably the reason why you cannot set the Request headers to null for the MSXML implementation but you can for the in-built object.

Therefore, I do not believe that there is any way to collectively clear all the headers. The link to Mibbit that you present only provides a function to set all the headers to null one by one. For normal scenarios, cutting down on headers may prove to be a very very insignificant of reducing traffic load.

That said, I am curious to know why you are setting the request headers to "." rather than an empty string "".

错爱 2024-07-21 13:07:35

我会放弃这种微观优化,转而研究推送模型。 首先:

两者这些通常与后端的 XMPP 服务器配对。

I would forgo this kind of micro-optimizations and look into a push model instead. By way of a starting place:

Both of these are typically paired with an XMPP server on the back-end.

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