Sunday, July 29, 2007

IE Caching issue with AJAX HTTP GET request

There is one very annoying issue with IE using AJAX when you use a HTTP-GET request (I haven't faced this problem with HTTP-POST request). I tried following but nothing seemed to work:

response.setHeader("Cache-Control","no-cache"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server

So we need to fool IE to consider the request as new request everytime, and you can fool IE by adding current timestamp at the end of your url. You can do following in your Javascript code:

url= url+"?currDate="+ new Date(); if your url is simple like /test/
url= url+"&currDate="+ new Date(); if your url contains request parameters and looks something like /test/

No comments: