tags:

views:

24

answers:

3

I am using Ajax to retrieve the data from server as below based on some ID to perform auto suggest function. however when i submit the form and update the database, the auto suggest field suppose should not contain anything for this ID anymore, but it will still retrieve data from its cache. do anyone know how to clear the cache and make the Ajax sending request to get the latest data from server every time i press the button? Pls help i really stuck on this whole weeks and couldnt find the solution.

For example: when ID field is 00001, auto suggest field will be 1,2,3. After i submit the form and update the database, when i search for 00001 again, it should not contain anything but it does, it still cache the data as 1,2,3 in suggest field...

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
}
else
 {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
var data=xmlhttp.responseText;
alert(data);

}
}
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
A: 

you could use http headers to prevent the response from being cached:

Cache-Control: no-cache
Expires: Mon, 24 Oct 2005 16:13:22 GMT

Another option is to add another parameter to the url that varies everytime (for example the current time in millis), that way for the browser you ask another url and the cache won't be used.

davyM
A: 

I had this problem once before. This is propabley something you can fix in your server settings. What the server does is get a server request, build the answer, and when the same request is done again it sends the same response it built before.

To easily avoid this problem, I added an extra request parameter (a UID). so: xmlhttp.open("GET","gethint.php?q="+str+"?something"=RANDOMGUID,true);

this way you always ha a unique request.

Nealv
A: 

Easiest thing to do is use jQuery#ajax and disable caching.

jQuery will suffix a parameter ?somenumber to your ajax call which just is sufficient to persuade the browser it cannot use cached data.

I came across this once. Here's the answer I got: http://stackoverflow.com/questions/2749560/why-does-jquery-ajax-add-a-parameter-to-the-url .


You could do the same thing manually too, but you would have to check if the addition of the parameter is all there is to it.

FK82