3

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?

Philipp Br
  • 113
  • 2
  • 7

2 Answers2

6

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

var url="//yoururl.com/";
url+="?"+new Date().getTime();
Okku
  • 7,468
  • 4
  • 30
  • 43
1

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?

Community
  • 1
  • 1