【问题标题】:Why does IE8 return cached result for consecutive identical XDomainRequest messages?为什么 IE8 会为连续相同的 XDomainRequest 消息返回缓存结果?
【发布时间】:2012-02-11 00:17:52
【问题描述】:

在我的应用程序中,我需要发出定期跨域 HTTP POST 请求以从服务器接收最新数据(定期轮询)。该应用程序在 Chrome 中时无法在 IE8 中运行。所以我决定用 Wireshark 调试一下:

我在 IE8 和 Chrome 中执行了 2 个等效代码。我用 Wireshark 监控我的网络。 wireshark 过滤器是:

http.request.full_uri == "http://www.andlabs.net/html5/uCOR.php"   

我注意到 IE8 只发送一次请求并为以下调用返回相同的缓存响应。而 Chrome 每次都会发送一个新请求。

我用于 IE8 的代码:

var cor = new XDomainRequest();
cor.onload = function() { alert(cor.responseText);}
cor.open('POST', 'http://www.andlabs.net/html5/uCOR.php');
cor.send();

我用于 Chrome 的代码:

var cor = new XMLHttpRequest();
cor.onload = function() { alert(cor.responseText);}
cor.open('POST', 'http://www.andlabs.net/html5/uCOR.php');
cor.send();

为了防止在 IE8 中缓存响应,我尝试了以下代码,它成功了:

var cor = new XDomainRequest();
cor.onload = function() { alert(cor.responseText);}
cor.open('POST', 'http://www.andlabs.net/html5/uCOR.php');
cor.send(''+new Date());

为什么 IE8 会出现这种行为,有什么方法可以以不同于我的方式解决这个问题?请注意,我不能对 GET 请求使用相同的技巧。

顺便说一下,IE的请求和响应如下:

请求:

POST /html5/uCOR.php HTTP/1.1
Accept: */*
Origin: http://jsbin.com
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
Host: www.andlabs.net
Content-Length: 0
Connection: Keep-Alive
Cache-Control: no-cache

回复:

HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/7.0
Access-Control-Allow-Origin: *
X-Powered-By: ASP.NET
Date: Tue, 17 Jan 2012 21:41:39 GMT
Content-Length: 180

This is a page from www.andlabs.net which is accessible from any website through Cross Origin Requests<br>This page contains the following header:<br>Access-Control-Allow-Origin: *

【问题讨论】:

  • 你能包含来自 uCOR.php 的响应头吗?
  • 添加了请求和响应的详细信息。

标签: html internet-explorer-8 xdomainrequest xmlhttprequest-level2


【解决方案1】:

响应中似乎没有任何缓存标头,因此浏览器的行为可能会有所不同。您可以在响应中添加以下标头吗:Cache-Control: no-cache

【讨论】:

  • 为什么有人要缓存帖子?修改服务器上的信息不违反POST的定义吗?
猜你喜欢
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2023-04-05
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2012-07-28
相关资源
最近更新 更多