为什么 IE 不让我看到带有 getResponseHeader()
的获取 Content-Length header ?
我知道那里正在发送 header ;我可以用 Wireshark 看到它们。 IE 只是不会让我得到它们。
如果没有发送 Content-Encoding header ,无论内容是否被 gzip 压缩,我都可以很好地得到它们。
示例代码:
function getXMLHttpRequest() {
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch (ex) {
return null;
}
}
}
function handler() {
if (oReq.readyState == 4 /* complete */) {
if (oReq.status == 200) {
// this alert will be missing Content-Length
// and Content-Encoding if Content-Encoding is sent.
alert(oReq.getAllResponseHeaders());
}
}
}
var oReq = getXMLHttpRequest();
if (oReq != null) {
oReq.open("GET", "http://www.example.com/gzipped/content.js", true);
oReq.onreadystatechange = handler;
oReq.send();
}
else {
window.alert("AJAX (XMLHTTP) not supported.");
}
最佳答案
您可能会注意到许多标题都没有,包括对您来说最重要的:“Content-Encoding: deflate”
这是因为根据 MVP 的说法,在解压 IE 后会生成假 header :
https://social.msdn.microsoft.com/Forums/ie/en-US/b2cc04ca-1d4e-4381-9750-361128987e2f/http-response-header-variable-returns-null-in-ie-11?forum=iewebdevelopment
关于ajax - IE 的 XMLhttpRequest 的 getResponseHeader ("Content-Length") 在发送 Content-Encoded 时不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3688871/