3

我有一个与以下问题非常相似的问题: 发送 Content-Encoded 时 IE's XMLhttpRequest's getResponseHeader("Content-Length") 不存在

我正在尝试获取特定 JS 文件的文件大小,它看起来像这样:

var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (!req) {
    throw new Error('XMLHttpRequest not supported');
}
req.open('HEAD', urlToScript, false);
req.send(null);
var size = req.getResponseHeader('Content-Length');

size 变量是 ie < 10 和nullie 10 中的空字符串。该代码在 FF 或 Chrome 等其他浏览器上运行良好。

这个问题似乎只有在请求的内容被压缩时才会出现。

在 IE 中,如果我用以下方式打印标题,则标题缺少“Content-Length”和“Content-Encoding” req.getAllResponseHeaders()

Server: Apache-Coyote/1.1
Last-Modified: Wed, 03 Mar 2010 07:01:40 GMT
Expires: Thu, 10 Apr 2014 12:03:27 GMT
Cache-Control: public, max-age=31536000
Content-Type: text/javascript;charset=utf-8
Date: Wed, 10 Apr 2013 12:03:26 GMT

Firefox 20 打印更多信息的地方:

Server: Apache-Coyote/1.1
Content-Encoding: gzip
Vary: Accept-Encoding
Last-Modified: Wed, 03 Mar 2010 07:01:40 GMT
Expires: Thu, 10 Apr 2014 12:05:24 GMT
Cache-Control: public, max-age=31536000
Content-Type: text/javascript;charset=utf-8
Content-Length: 17669
Date: Wed, 10 Apr 2013 12:05:24 GMT
X-Cache: MISS from somewhere.com
Via: 1.1 somewhere.com:8001 (squid/2.7.STABLE9)
Connection: keep-alive
Proxy-Connection: keep-alive

我还尝试将“Content-Type”设置为“application/javascript”并手动设置“Accept-Encoding”请求标头。没有任何效果。有任何想法吗?

4

0 回答 0