AJAX Refresh Problem w IE Not Refreshing

I’ve been doing a lot of converting of web pages that required the user to refresh the page for updated content to AJAX (eliminates need to refresh). It was all good until I started receiving complaints that data was no longer refreshing in IE.

What I’m using is a AJAX get command like this:

xmlhttp.open(“GET”,”url”,true);
xmlhttp.send();

however, in Internet Explorer, the content never gets refreshed – that is, the AJAX request is getting cached. There are a number of suggested solutions, including using a POST rather than a GET and appending a random string to the request.

The best way to keep AJAX working in IE is to tell the xmlhttp object not to cache content that is older than a certain date; you can do that by changing the request in this fashion (appending 1 line):

xmlhttp.open(“GET”,”url”,true);
xmlhttp.setRequestHeader(“If-Modified-Since”, “Sat, 1 Jan 2000 00:00:00 GMT”);
xmlhttp.send();

This will solve any AJAX caching problem you might encounter. It’s a simple solution, but caused me a big headache!

Also, if you have webpages with ad code on them, each refresh is another impression and can skew your results more than you think! Move to AJAX and you’ll solve that problem :)

I hope this helps those of you playing with AJAX.

12 Comments

  1. Ganesh says:

    Thanks. Your big headache reduced mine.

  2. Mark says:

    Thanks so much for letting us know how to fix this!

  3. Conrad says:

    Ummm…why not set cache-control in the response header? The following should work very nicely Cache-Control: no-cache

  4. Vincet says:

    Thanks a bunch pal !!!

  5. Maddy257 says:

    Thanks you so much, Solved my prob…..

  6. Roberto Rutilo says:

    Thank you !!

  7. dhiman says:

    Thank you

  8. Wilson says:

    Brilliant, Thank you!

  9. Chetan says:

    Thanx a mill. it works :)

  10. Jim says:

    Glad I could help Mark and Dillen :)

    Jim

  11. Mark Love says:

    THANK YOU!

  12. Dillen says:

    Thanks, I’ve been searching for this for a very long time, thanks for your help.

Speak Your Mind