xmlhttp緩衝清除的2種解決方案_javascript技巧

來源:互聯網
上載者:User

方法一:加If-Modified-Since頭
xmlhttp多次調用時它卻總是顯示快取頁面面, 嘗試在 php 或 asp 中加入相應的http頭明確不要緩衝, 也沒什麼效果!!

現在終於找到一個辦法啦,就是在 xmlhttp.open 之後發送一個If-Modified-Since頭即可, 代碼如下
xmlhttp.setRequestHeader('If-Modified-Since', '0');
xmlHttp.onreadystatechange = stateChanged;
            xmlHttp.open("GET", url, true);
            xmlHttp.setRequestHeader('If-Modified-Since', '0');
            xmlHttp.send(null);

方法二:請求URL後加變化參數

因為IE預設將相同的URL請求進行緩衝,所以URL加上唯一區分的參數作為區分,保持每次請求的URL不同,即可繞過IE緩衝的問題
      js:     URL = "http://host/a.php"+"?"+Math.random();
      vbs:   URL = "http://host/a.php"&"?"&Timer()

 //var url = "ResponseContentPageScore.aspx?key=" + Math.round(Math.random() * 100) + "&HotelCD=" + inputContent;
 var url = "ResponseContentPageScore.aspx?key=" + new Date().getTime() + "&HotelCD=" + inputContent;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.