6

I have an php powered application with javascript and many jax calls. my application is working upto date in firefox. but when i run it in internet explorer-8 or similar versions my ajax call gets cached in my browser so i am not able to output the upto date info with the ajax calls instead the result for that ajax calls are served with old data's which reside in the browser cache.

 I have tried lots of possible options as listed below

1.) I added following meta tag in header files


<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>

2.)I added Following php code

header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Pragma: no-cache');

But still the above 2 approaches did not solve my problem ie, please can anybody help me to disable caching internet explorer when my application runs, so that its possible to get upto date information.

thanks in advance

4
  • The second solution with HTTP headers should work. Just make sure the headers are really being sent (use web-sniffer.net), clear the browser's cache (files were probably cached when there were no headers sent) and check it once again. Commented Aug 5, 2011 at 9:40
  • I added Following php code -- in which file? The response headers should go in your AJAX script. Commented Aug 5, 2011 at 10:09
  • @duri I don't believe that solution will work if an item is already in cache. I think you might be able to hard refresh using javascript => stackoverflow.com/questions/2099201/…. The rest of the items I think you should version using something like filemtime() Commented Aug 5, 2011 at 14:07
  • @Alfred I'm aware it might not work if files were already cached. This is why I recommended clearing the browser's cache. Commented Aug 5, 2011 at 14:44

1 Answer 1

9

Make each AJAX request unique in some way. That will prevent IE from caching the response.

For example, if your normal AJAX query URL is www.mysite.com/ajax.php?dog=cat, add in a querystring parameter to each AJAX request that is unique:

www.mysite.com/ajax.php?dog=cat&queryid=1

Increment that parameter each time you make an AJAX request, and that should hopefully do the trick for you.

Sign up to request clarification or add additional context in comments.

10 Comments

ya i have done each ajax request unique by adding the current time with seconds but i am searching for a better way to get a solution generic for all my files and all requests can anybody please help me.
url + "&rand="+Math.floor(Math.random()*11)
Why use up processor time by using Math.random? A simple increment will do the trick and isn't as wasteful.
gowtham - I can't think of a more simple solution than simply appending an incremented int each time you make a request. Many Javascript libraries do something similar.
use filemtime() => php.net/manual/en/function.filemtime.php. incrementing is also expensive because you need to keep track of counter via database. filemtime() is just simple system call. The best would be even to do this once at file creation!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.