简体   繁体   English

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

[英]How can I clear HTTP headers for AJAX GET calls?

I have developed a solution that relies on an AJAX call to retrieve information and update the client page every 10 seconds. 我开发了一种解决方案,该解决方案依赖AJAX调用来每隔10秒检索一次信息并更新客户端页面。 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. 我在服务器端删除了许多冗余标头,这些标头大多与ASP.NET有关,现在我试图减少来自客户端的标头。

The browser used by my company is IE (version 6, to be upgraded to 7 soon). 我公司使用的浏览器是IE(版本6,即将升级到7)。 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. 尽管我读过可以清除其中一些标头,但我还没有找到一种可在IE6中使用的方法。 Setting them to null results in a Type Mismatch exception, so I've ended up just replacing them with '.' 将它们设置为null会导致Type Mismatch异常,因此我最终只是将它们替换为'。 for the time being. 暂且。 Is there another way of clearing them or an alternative method of reducing the submitted HTTP headers? 是否有清除它们的另一种方法或减少提交的HTTP标头的替代方法?

Also, there seems to be no way of replacing or shortening the 'Referrer' header at all. 另外,似乎根本没有办法替换或缩短“ Referrer”标头。

According to the WD spec 根据WD规范

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers. 如果作为参数提供的HTTP标头已经是请求标头列表的一部分,则setRequestHeader()方法将附加一个值。

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. 似乎根本没有办法替换或缩短'Referrer'标头。

That wouldn't surprise me, given that some people misguidedly use 'Referer' [sic] as an access-control mechanism. 鉴于有些人错误地使用“ Referer”(原文如此)作为访问控制机制,这对我来说并不奇怪。

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. 您可以尝试确保当前页面的URL不会过长,但是老实说,所有这些对我来说都是过早优化的味道。 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. 无论您做什么,您的请求都将适合一个IP数据包,因此gonig不会有明显的性能差异。

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. Mibbit(如您所链接的博客所述)可能值得尝试一下,因为Mibbit吸引了大量的流量,但是对于一个简单的公司范围的应用程序,我不认为跨浏览器和- proxy-testing-burden:最终用户与标题混淆的比率是值得的。

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 . IE 6和旧版本使用从创建的ActiveXObject MSXML.XMLHTTP (实际上源自IXMLHTTPRequest ),而IE 7和其他现代浏览器如Mozilla使用称为本征对象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. 这可能是为什么不能将MSXML实现的Request标头设置为null ,而对于内置对象却可以将其设置为null的原因。

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. 您提供的指向Mibbit的链接仅提供一种将所有标头设置为空的功能。 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 "" . 而不是空字符串""

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. 通常,这两者都与后端的XMPP服务器配对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM