【问题标题】:How to let XMLHttpRequest not cach in IE如何在 IE 中获取未缓存的 XMLHttpRequest
【发布时间】:2013-05-07 00:39:04
【问题描述】:
if(xmlhttp) { 
xmlhttp.open("GET","DokterWeek_KlantoverzichtServletAjax?" +   $(this).prop("href").split("?")[1],true);//gettime will be the servlet name
xmlhttp.onreadystatechange  = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(null);
 }

});
});



function getXMLObject()  //XML OBJECT
{
var xmlHttp = false;
try {
 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
  }
  catch (e) {
   try {
   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
   }
   catch (e2) {
     xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
     }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera,chrome Browsers
   }
    return xmlHttp;  // Mandatory Statement returning the ajax object created
   }





  var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object


     function handleServerResponse() {

   document.getElementById("pop1").innerHTML = xmlhttp.responseText; //Update the HTML              Form element 

  }

你好, 我有以下问题(代码有效),当我使用 firefox 或 Chrome 时,xmlhttpRequest(ajax-call) 会刷新(所以效果很好)。但是 IE 9.0+ 缓存了 XMLHttprequest,所以它永远不会刷新。我在互联网上阅读了很多关于这个问题的信息,但我真的找不到任何解决这个问题的方法。

谁能告诉我解决这个问题的可能性? 我认为使用 jquery ajax 可以解决这个问题,但我没有头脑将整个脚本调整为 jquery。有人说您可以将实时时间设置为 0,但我没有找到。 (我用一个 servlet 与我的 Ajax 通信) 有人知道一个清晰、更简单的解决方案吗?

非常感谢

【问题讨论】:

    标签: java javascript jquery ajax xmlhttprequest


    【解决方案1】:

    一种公认的做法是在 url 中添加一个随机参数,例如时间戳。

    _d之类的参数添加到值为new Date().getTime()的url

    例如:

    xmlhttp.open("GET","DokterWeek_KlantoverzichtServletAjax?_d=" + (new Date().getTime()) + '&' +   $(this).prop("href").split("?")[1], true);//gettime will be the servlet name
    

    【讨论】:

    • Arun P Johny,再次感谢您的精彩回答!真的很完美
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2015-03-25
    • 2015-01-06
    • 2017-09-09
    • 2023-02-13
    • 2011-01-09
    • 2015-12-02
    相关资源
    最近更新 更多