【问题标题】:XMLHttpRequest in IE-7IE-7 中的 XMLHttpRequest
【发布时间】:2012-07-13 04:19:01
【问题描述】:

我正在按如下方式创建 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;
}

对象返回的响应取决于操作类使用的数据库。

这在 Firefox 10.0 中运行良好。

但对于 IE7,它仅在第一次返回正确响应。对于其余的函数调用,它返回相同的响应(无论我们在数据库中进行什么更改)。仅当我关闭选项卡并打开它时(甚至在重新加载页面时),它才会更新其响应。

如何让它在 IE 7 中运行?

【问题讨论】:

    标签: javascript xml internet-explorer-7 xmlhttprequest


    【解决方案1】:

    听起来响应正在被缓存。

    在 URI 的末尾添加一个伪随机字符串(例如时间戳)以缓存突发。

    【讨论】:

    • 成功了.. 谢谢... 我将 open 调用转换为 xmlhttp.open("GET","checkFormDependency.action?formName="+formName+"&applicationName="+appName+"&timeStamp= "+new Date().getTime(),false);
    【解决方案2】:

    您只是在使用 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
     }
    
    }
    

    因此 IE 7 每次都会找到一个空引用,并且每次使用都需要重新创建它。

    如果您不想每次都在 XMLHTTPRequest 中使用一些 HTTP-Headers 时创建和删除它

    xmlHttp_global.setRequestHeader("If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT");
    xmlHttp_global.setRequestHeader("Cache-Control", "no-cache");
    

    点赞推荐here

    其他选择包括:

    • 使用 POST 方法而不是 GET 方法

      xmlHttp_global.open("POST","checkFormDependency.action",false); xmlHttp_global.setRequestHeader("内容类型","application/x-www-form-urlencoded"); // 或其他内容类型,由您决定 xmlHttp_global.send("formName="+formName+"&applicationName="+appName);

    • 在查询字符串中使用“虚拟”变量来爆出 IE(7,6) 的缓存器

      xmlHttp_global.open("GET","checkFormDependency.action formName="+formName+"&applicationName="+appName+"randomVar="+Math.Random(),false);

    链接

    【讨论】:

    • 我在本地创建了我的变量。但是您已经为全局 XMLHttpRequest 变量编写了代码。有必要让它全球化吗?我也尝试使用 abort() 但它没有用。我最后也尝试了 xmlhttp = null 但我认为它没有任何意义,因为它是一个局部变量。
    • 看你有 2 个变量,一个在创建函数中的本地 var xmlHttp 和一个接收创建函数结果的全局 xmlHttp_global。你可以在方便的时候和他们一起玩。 xmlHttp_global = null 必须在你在回调中使用它之后设置,而不是在任何地方或无论如何......如果你仍然有一些问题,只需尝试在我的更新中提出的替代方案(例如使用请求标头)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2012-07-16
    • 2017-02-06
    • 2015-02-06
    • 2011-01-09
    相关资源
    最近更新 更多