by Andrew Potter
Another symptom is that this problem persists even when one adjusts the header values returned by the ajax call. For instance I had the following header values which were to be returned via a PHP Ajax server routine.
After much googling I changed to PHP code so that it returned headers which should have informed the client that the returned data should not be cached but be re-read every time. The code looked like this.
$now = gmdate('D, d M Y H:i:s') . ' GMT';
header("Cache-Control: no-cache, must-revalidate");
xmlhttp.open("GET", "ajaxservice.php?paramtervalue=2", true);
Even if the the data on the server side change from call to call some browsers will not return the changed data from the server but rather the cahced data stored by the browser on the client. Ths solution is to add an extra dummy parameter to the URL in the xmlhttp.open call which makes the subsequent URL to differ from the previous call. This makes the browser think that URL has changed and forces it to actually perform a call to the server and retrieve the updated data. A widely used solution is to append a random number to the URL as a dummy parameter. This has no effect on server side since the parameter value is ignored. An example would be the following.
xmlhttp.open("GET", "ajaxservice.php?paramtervalue=2&dummy="+Math.random(), true);
This solution is not only relevant for the URL in the Ajax call but can also be used in later references to data on the server. In one situation I had a constellation in which the call to the Ajax server changed an image file on the server so that it had different contents albeit using the same name. The client referenced this binary file directly using the following code.
This approached suffered from the same caching phenomenon as the previous examples of the Ajax call using xmlhttp.open. It was solved by using the same technique, i.e. using a dummy parameter to trick the browser ionto thinking that it must perform a call to the server to obtain the data. The solution is similar.