最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX in Internet Explorer 11 - Don't cache - Stack Overflow

programmeradmin3浏览0评论

I have an issue with AJAX in the IE 11. My page is asking sone values via AJAX form the server using this code:

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function ()
{
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
  {
    doSomeThing();
  }
}
xmlhttp.open("GET", "theURL", true);
xmlhttp.send();

In Chrome and Firefox it's working fine but the IE seems to cache the AJAX response and I get the same result, even if the page on the server changed.

Is there a way to disable the caching?

I have an issue with AJAX in the IE 11. My page is asking sone values via AJAX form the server using this code:

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function ()
{
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
  {
    doSomeThing();
  }
}
xmlhttp.open("GET", "theURL", true);
xmlhttp.send();

In Chrome and Firefox it's working fine but the IE seems to cache the AJAX response and I get the same result, even if the page on the server changed.

Is there a way to disable the caching?

Share Improve this question asked Jun 17, 2015 at 9:36 Philipp BrPhilipp Br 1132 silver badges7 bronze badges 3
  • append a random number on your ajax request/respond url – Keith A Commented Jun 17, 2015 at 9:40
  • or just fix your server to not tell your client to cache. – John Dvorak Commented Jun 17, 2015 at 9:40
  • 1 Also, don't use w3schools as a resource. Or else we'll be supporting IE5 'till the end of time (and no, supporting IE5 isn't by far the worst offence of theirs). – John Dvorak Commented Jun 17, 2015 at 9:41
Add a ment  | 

2 Answers 2

Reset to default 6

Add a random parameter to the url, such as a timestamp:

var url="//yoururl./";
url+="?"+new Date().getTime();

This was driving me crazy. I tried many cache busting techniques and setting cache headers. So many of these either did not work or were wild goose chases. The only solution I found which tested to work correctly was setting:

Header Pragma: no-cache

I hope it saves others with IE headaches.

BTW this is StackOverflow thread is great to shed light on the difference between Pragma and Cache-control: Difference between Pragma and Cache-control headers?

发布评论

评论列表(0)

  1. 暂无评论