我正在创建XMLHttpRequest,如下所示:
function checkDependencyFormFilledStatus(appName,formName){
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","checkFormDependency.action formName="+formName+"&applicationName="+appName,false);
xmlhttp.send();
var dependentFormEmptyStatus = Ext.JSON.decode(xmlhttp.responseText);
alert(xmlhttp.responseText);
return dependentFormEmptyStatus;
}对象返回的响应取决于操作类使用的数据库。
这在Firefox10.0中运行得很好。
但是对于IE7,它只在第一次返回正确的响应。对于其余的函数调用,它返回相同的响应(无论我们在数据库中做了什么更改)。只有当我关闭选项卡并打开它时,它才会更新它的响应(即使在重新加载页面时也不会)。
如何让它在IE7中工作?
发布于 2012-07-13 18:11:39
听起来响应被缓存了。
在URI的末尾添加一个伪随机字符串(例如时间戳),以缓存突发。
发布于 2012-07-13 18:45:15
您只是遇到了IE7的缓存问题,因为它在创建XMLHttpRequest()之后将其缓存,并将其存储在内存中。即使使用后继的xmlhttp=new XMLHttpRequest();,变量也不会得到任何赋值,因为它已经有了一个实例(来自您的第一个xmlhttp=new XMLHttpRequest(); )。
您需要做的是在每次使用之后使您的XMLHttpRequest请求无效并销毁。
首先创建您的XMLHttpRequest (针对msie 7),如下所示:
function createXMLHttpRequest(){
var xmlHttp = null;
if(typeof XMLHttpRequest != "undefined"){
xmlHttp = new XMLHttpRequest();
}
else if(typeof window.ActiveXObject != "undefined"){
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
}
catch(e){
try {
xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e){
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
xmlHttp = null;
}
}
}
}
return xmlHttp;
}所以每次都要在你想使用的函数中创建它。
function checkDependencyFormFilledStatus(appName,formName){
if(xmlHttp_global){
xmlHttp_global.abort(); // abort the current request if there's one
}
// Create the object each time a call is about to be made
xmlHttp_global = createXMLHttpRequest();
if(xmlHttp_global){
xmlHttp_global.onreadystatechange = myCallbackFunction; // make you callback thing here
xmlHttp_global.open("GET","checkFormDependency.action formName="+formName+"&applicationName="+appName,false);
xmlHttp_global.send(null);
}
}在你的回调("onreadystatechange“函数)中,你在使用它之后删除它(
function myCallbackFunction()
{
if(xmlHttp_global && xmlHttp_global.readyState == 4){
//do your thing here and ... or nothing
var dependentFormEmptyStatus = Ext.JSON.decode(xmlhttp.responseText);
alert(xmlhttp.responseText); // like this for example?
xmlHttp_global = null; //delete your XMLHTTPRequest
}
}因此,IE7每次都会发现一个空引用,并且每次使用时都需要重新创建它。
如果您不想每次创建和删除它,只需在XMLHTTPRequest中使用一些HTTP头即可
xmlHttp_global.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
xmlHttp_global.setRequestHeader("Cache-Control", "no-cache");就像建议的here
另一种选择包括:
xmlHttp_global.open("POST","checkFormDependency.action",false);xmlHttp_global.setRequestHeader("Content-type",“应用程序/x-www-form-urlencoded”);//或者其他内容类型,这取决于您在查询字符串中使用一个“xmlHttp_global.send("formName="+formName+"&applicationName="+appName);
false(“GET”,“formName="+formName+"&applicationName="+appName+"randomVar="+Math.Random(),xmlHttp_global.open checkFormDependency.action);
链接
https://stackoverflow.com/questions/11468379
复制相似问题