Jaslabs: High performance Software

High Performance Software

How to stop IE from caching AJAX requests

by Justin Silverton

While working on an AJAX project over the weekend, I ran into the following issue: (through a GET request), every time I tried to call a certain function, It was returning the same data (which was supposed to be different each time)

I first tried the following (which should disable browser caching):

(in PHP)

header( “Expires: Mon, 26 Jul 1997 05:00:00 GMT” );
header( “Last-Modified: ” . gmdate( “D, d M Y H:i:s” ) . ” GMT” );
header( “Cache-Control: no-cache, must-revalidate” );
header( “Pragma: no-cache” );

The data still did not change.

I finally came to the following solutions:

1) use a POST request. When using with xmlhttprequest, it is slightly more complicated.

2) add a unique identifier to the end of my GET url.

I choose #2. A unique Identifier can be created using the current data+time. Here is a simple way to generate this (in Javascript):

var date = new Date();
var timestamp = date.getTime();

createXMLHttpRequest();
xmlHttp.onreadystatechange = handleMessages;
xmlHttp.open(”GET”,”script.php?time=”+timestamp,true);
xmlHttp.send(null);

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

23 Comments so far

  1. Richard Howard February 5th, 2007 11:24 am

    Been using the header approach for a while w/o any problems. How thoroughly did you test?

  2. Martin February 5th, 2007 1:22 pm

    Thanks a lot for this hint! I encountered exact this problem this morning & was just going to hunt for a solution.

  3. Alex February 5th, 2007 10:00 pm

    You can combine both =)

  4. Justin Silverton February 5th, 2007 11:15 pm

    “Been using the header approach for a while w/o any problems. How thoroughly did you test?”

    I tried it with IE 5,6, and 7. 5 and 6 it worked most of the time and 7 not at all.

  5. Satya Prakash Karan February 6th, 2007 4:03 am

    Yes this is a problem with IE.
    Through serverside insertion of time, this can be done easily-

    xmlHttp.open(”GET”,”script.php?time=”,true)

  6. Adithya February 6th, 2007 4:41 am

    Thanks for the valuable hint. It’s good.

  7. MA Razzaque Rupom March 2nd, 2007 10:47 pm

    Hi, This is really helpful. I used this technique while I was facing problems with IE. And yes I am done. Thanks a lot. Keep up your great works.

    Regards.

    Rupom
    http://www.rupom.info

  8. nice idea March 28th, 2007 10:57 am

    still can’t cure the cache problem though, even if I use both techniques. My script works fine in firefox, but remains static in IE

  9. Justin Silverton March 28th, 2007 11:19 am

    POSTS are not cached in any browser. You may want to just pass your data that way if you are still having caching issues.

  10. Shyam Sundar April 10th, 2007 3:22 am

    One another method is using Math.rand() * 10000;

    :-)

  11. mamund April 23rd, 2007 12:53 am

    the msdn site sez this is due to pre-check/post-check local cache behavior built into ie. i added the following response header and it worked:

    Cache-Control: post-check=1,pre-check=2

    check out the details here:
    http://msdn.microsoft.com/library/default.asp?url=/workshop/author/perf/perftips.asp

  12. Robertus May 18th, 2007 3:13 pm

    I have been using the unique identifier approach successfuly for quite awhile, but it always botherd me that those who poke around IE cache can see the the url requests with all the data in it. Converting to POST or encrypting data would be a solution, but it felt like overkill. The following took care of it in my case:

    header(”Cache-Control: post-check=1,pre-check=2″);
    header(”Cache-Control: no-cache, must-revalidate”);
    header(”Pragma: no-cache”);

    Thanks for the hints.

  13. DigitalSaigon May 18th, 2007 4:24 pm

    Thanks Justin. It works great. I have apply this to load xml using JavaScript for my SharePoint website.

    Load XML using JavaScript in SharePoint here: http://www.digitalsaigon.com/Lists/Tech%20Tricks%20Tips%20Code%20Snippet/DispForm.aspx?ID=23

  14. RAVI NAIR August 2nd, 2007 6:49 pm

    the best way to do this is
    ——————————————————–
    After the open for your “GET” and before the send method insert the following code:

    req.setRequestHeader(”If-Modified-Since”, “Sat, 1 Jan 2000 00:00:00 GMT”);
    req.setRequestHeader(”Cache-Control”, “no-cache”);

    Of course, use yourclassname insted of “req”.

    These headers will solve your problem for IE and Firefox.
    —————————————————-
    RAVI NAIR

  15. Justin Silverton August 2nd, 2007 10:00 pm

    RAVI,

    Thanks for the tips!

  16. RAVI NAIR August 3rd, 2007 2:28 am

    Hi Justin,

    Let me know if it works for you. It does for me.

  17. Justin Silverton August 3rd, 2007 7:34 pm

    “Let me know if it works for you. It does for me”

    It worked for me.

  18. hari September 6th, 2007 9:37 am

    Hi Justin,
    Thanks a lot, its working !!

  19. Geo September 6th, 2007 10:38 pm

    Hi Guys,
    I did try all methods listed above, and still in IE the content is being cached? Have these methods been tested on IE7?

  20. gupteshwar October 25th, 2007 3:34 am

    In my case page keeps shuffling thumbnails at specific interval for which I have used xmlhttp method which works fine in Firefox but couldn’t work that successfully in IE (means it changes images only once thats it, after that changes happened with same data basically cache problem).
    I tried both methods but unfortunately not fruitfull.
    Anyway thanx man.

  21. Faisal Abdul Moid December 20th, 2007 5:59 am

    Thanks for the solution, Justin, approach #1 i.e header is working fine in IE, (checked in IE 7)

  22. Polar December 30th, 2007 10:00 pm

    Tested several methods to solve this problem for IE7 and the solution of RAVI NAIR works like a charm! Many thanks! All the other methods found on the web did not work for me! Thanks again!

  23. Shimul February 24th, 2008 5:18 pm

    WOW!! What great trick!!!

    I uses both of them but finally i preferred to use RAVI’s solution. Because it seems redundant when I’m sending time over GET variable and apply header on that page. Anyways Justin’s solution was rocking!!!

    It saves lot of my effort! I uses this technique on http://www.StockBangladesh.com.

    Thanks

Leave a reply