如何清除AJAX GET调用的HTTP头? [英] How can I clear HTTP headers for AJAX GET calls?

查看:125
本文介绍了如何清除AJAX GET调用的HTTP头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一种解决方案,它依赖于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'标题根据 #setrequestheaderrel =nofollow noreferrer> WD规范


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


也就是说,你只能添加标题,而不是替换标题。 / p>

这与当前的浏览器行为并不完全匹配,但它可能是浏览器的发展方向,在这种情况下,这方面的任何努力都是浪费时间术语。在任何情况下,设置标题的当前浏览器行为都是多种多样的,通常无法依赖。


似乎没办法完全替换或缩短'Referrer'标题。


鉴于有些人误导了'Referer',我不会感到惊讶[原文如此]作为一种访问控制机制。



您可以尝试确保当前页面网址不会过长,但说实话这一切都是过早优化给我。无论你做什么,你的要求都适合一个IP数据包,所以没有明显的性能差异。



对于Mibbit来说可能是值得的(如上所述)在您链接的博客上尝试这些东西,因为Mibbit的流量非常惊人,但对于一个简单的公司范围的应用程序,我不认为跨浏览器和代理测试负担:最终用户 - 弄乱标题的好处是值得的。


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.

解决方案

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.

这篇关于如何清除AJAX GET调用的HTTP头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆