2012-07-13 83 views
2

我創建的XMLHttpRequest如下:的XMLHttpRequest在IE-7

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中工作?

回答

5

聲音像響應正在被緩存。

在URI的末尾添加僞隨機字符串(例如時間戳)以緩存突發。

+0

它的工作..謝謝你...我轉換的開放調用xmlhttp.open( 「GET」,「checkFormDependency.action?表格名稱= 「+ formName +」&applicationName =「+ appName +」&timeStamp =「+ new Date()。getTime(),false); – Shashwat 2012-07-13 10:45:54

3

您只是在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將每次找到一個空引用刪除後它,將有必要再重新創建每次使用。

,如果你不希望創建和刪除eacht時候你只是一些HTTP報頭在你的XMLHTTPRequest發揮

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

像建議here

另一種替代方案包括:

  • 通過GET方法使用POST方法

    xmlHttp_global.open(「POST」,「checkFormDependency.action」,false); xmlHttp_global.setRequestHeader(「Content-type」,「application/x-www-form-urlencoded」); //或其他內容類型,由您決定 xmlHttp_global。發送(「formName =」+ formName +「& applicationName =」+ appName);

  • 使用在查詢字符串一個 「虛擬」 可變爆出IE的cacher的(7,6)

    xmlHttp_global.open( 「GET」, 「checkFormDependency.action表格名稱=」 +表格名稱+」 & applicationName =「+ appName +」randomVar =「+ Math.Random(),false);

鏈接

+0

我在本地創建了我的變量。但是你已經編寫了全局XMLHttpRequest變量的代碼。是否有必要使其成爲全球?我也嘗試使用abort(),但它不起作用。我也在最後嘗試了xmlhttp = null,但我不認爲它是有意義的,因爲它是一個局部變量。 – Shashwat 2012-07-13 10:58:56

+0

看起來你有2個變量,一個是創建函數中的局部var xmlHttp,另一個是全局xmlHttp_global,它接收你的創建函數的結果。你可以在你方便的時候和他們一起玩。 xmlHttp_global = null必須在回調函數中使用後設置,而不是在任何地方或任何方式......如果您仍然遇到一些問題,請嘗試在更新後提出的替代方案(例如使用請求標頭) – arthur 2012-07-13 11:06:20