XMLHttpRequest in IE-7

柔情痞子 提交于 2019-12-24 01:54:51

问题


I'm creating XMLHttpRequest as follows:

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;
}

The response returned by the object is dependent on the database which the action class is using.

This works fine in Firefox 10.0.

But for IE7, it returns correct response only for the first time. And for the rest of the function calls, it returns the same response (no matter what changes we make in the database). It updates its response only when I close the tab and open it (not even on reloading the page).

How to make it work in IE 7?


回答1:


Sounds like the response is being cached.

Add a psuedo-random string (e.g. a timestamp) to the end of the URI to cache burst.




回答2:


you are simply having a caching issue with IE7, as it caches the XMLHttpRequest() after it created it and store it in its memory. even with subsequents xmlhttp=new XMLHttpRequest(); the variable don't get any assigment because it already has an instance (from your first xmlhttp=new XMLHttpRequest(); ) .

what you need to do is to invalidate and destroy your XMLHttpRequest request after every use.

you first create your XMLHttpRequest (for msie 7) like this:

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;
}

so to create it each time in the function you want to use.

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);
    }
}

in your callback ("onreadystatechange" function) you delete it after using it

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
 }

}

so IE 7 will find each time an empty reference and will have the need to recreate it again for each use.

if you don't want to create and delete it eacht time you simply play with some HTTP-Headers in your XMLHTTPRequest

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

like suggested here

Another alternatives include:

  • Using POST method over GET method

    xmlHttp_global.open("POST","checkFormDependency.action",false); xmlHttp_global.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // or another content type , its up to you xmlHttp_global.send("formName="+formName+"&applicationName="+appName);

  • Using a "dummy" variable in your query-String to burst out the cacher of IE(7,6)

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

Links

  • XMLHTTPRequest cache in IE 7 and 6


来源:https://stackoverflow.com/questions/11468379/xmlhttprequest-in-ie-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!