Guillermo Phillips
Guillermo Phillips

Reputation: 2216

PHP Ajax Caching for IE7 and iE8

I have an application written in PHP/Javascript which uses AJAX extensively. I am concerned that the default caching behaviour for IE7 and IE8 set for our organisation, of 'Automatic' will scupper my application.

There are approximately 1500 users and my IT department say that they won't change the caching option in IE for all those users.

My question is: How can I absolutely guarantee that if I make a change to my application, that all users will immediately see that change?

Also, how can I guarantee that AJAX will always bring back fresh results? Do I really have to resort to making all my URLs unique for every call?

There seems to be a fair amount of uncertainty on this topic on the internet. There must be a definitive answer that always works.

More Questions

Why doesn't just setting the HTTP headers in the AJAX files do the trick?

Also, how do I know that these solutions really work? What is the correct procedure for testing caching behaviour?

Upvotes: 2

Views: 1691

Answers (5)

Sam Dutton
Sam Dutton

Reputation: 15299

With jQuery you can add an extra parameter to avoid caching like this:

$.getJSON(myUrl, {"no_cache": new Date().getTime()}, function(data) {
// do something
}


$.getJSON(myUrl, {"no_cache": new Date().getTime()}, function(data) {
// do something else
}

Upvotes: 0

jensgram
jensgram

Reputation: 31518

Use POST request instead since the data on the server obviously changes (otherwise you wouldn't have a problem with caching). See http://javascript.about.com/od/ajax/a/ajaxgp.htm

Upvotes: 0

Lizard
Lizard

Reputation: 45062

Try adding these headers:

header("Cache-Control: no-cache, must-revalidate");
header('Pragma: no-cache');

Also maybe pass a rand number and or time() in the query string?

Upvotes: 1

Andrew
Andrew

Reputation: 6351

If you are going to append a cache busting unique id to the end of every query then don't use rand. Use the current date/time instead (in ms). That guarantees uniqueness, also more convenient to debug.

Upvotes: 3

gargantuan
gargantuan

Reputation: 8954

making the URL unique isn't a massive overhead. You simply need to add a random number to the end of the URL. IN the case of GET requests, you can do it like this

/someresource.php?some_key=foo&no_cache=9832742938642

You can just ignore the rand key value pair in whatever script is processing the request.

As far as I know, that's the only option you have.

Upvotes: 0

Related Questions