Alone as an Island What’s the pride in being a Programmer?
Mar 06

Internet Explorer and a few other browsers cache GET requests for faster browsing.  Good for normal pages. Bad for AJAX. So, for repeated AJAX requests, you will get a cached data instead of the actual response from the server-side.  (Firefox, the prince of browsers does not seem to have this problem).

Two Options come up for this problem.

1) Use POST method for AJAX requests or

2) Just append some dummy parameter which has a different value for different GET requests. Just gives the browser an impression that its a fresh request.

So, Instead of this

var url = ‘getProducts.jsp?productId=123′

use this:

var url = ‘getProducts.jsp?productId=123′  + “&dummy=” + new Date().getTime();

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: ,

No Responses to “Avoid caching in Internet Explorer for AJAX requests”

  1. tchibo Says:

    Hi!
    Recently I’ve got this problem.
    One solution would be to send some headers before loading the ajax GET page.
    Ex.
    header(”Cache-control: private, no-cache”);
    header(”Expires: Mon, 26 Jun 1997 05:00:00 GMT”);
    header(”Pragma: no-cache”);

  2. Arun Says:

    Thanks a lot for your inputs Victor.
    As always, IE has another bug for this header setting. So, IE requires your code to be put in the bottom of the page too.. after the body tag.

    [ page stuff here ]

    Meta tag Img

Leave a Reply

Give your best to the world.