2012-01-15 40 views
0

在我的应用程序中,我需要定期进行跨域HTTP POST请求以接收来自服务器的最新数据(定期轮询)。当它在Chrome中时,该应用程序无法在IE8中运行。所以我决定用Wireshark进行调试:为什么IE8为连续相同的XDomainRequest消息返回缓存结果?

我在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的行为是这样,有什么办法可以解决这个问题的方式不同于wha我做过了吗?请注意,我不能对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: * 
+0

您可以包含来自uCOR.php的响应标头吗? – monsur 2012-01-16 14:21:39

+0

添加了请求和响应详细信息。 – CEGRD 2012-01-17 21:54:49

回答

2

看起来没有任何缓存头在响应中,浏览器可能会有不同的表现。您可以将下列标题添加到响应中:Cache-Control: no-cache

+2

为什么有人想缓存邮件?它是否违反了POST的定义,它修改了服务器上的信息? – CEGRD 2012-01-18 07:14:54