使用ajax时如何清除缓存数据? [英] how to clear the cache data when using ajax?

查看:59
本文介绍了使用ajax时如何清除缓存数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajax根据某些ID从服务器检索数据,如下所示,以执行自动建议功能.但是,当我提交表单并更新数据库时,自动建议"字段应该不再包含此ID的任何内容,但仍会从其缓存中检索数据.有谁知道如何清除缓存并在每次按下按钮时发出Ajax发送请求以从服务器获取最新数据?请帮助我整个星期都坚持下来,找不到解决方法.

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.

例如:当ID字段为00001时,自动建议字段将为1,2,3.在我提交表单并更新数据库后,当我再次搜索00001时,它不应该包含任何内容,但是包含内容,它仍将数据作为1,2,3缓存在建议字段中.

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();

推荐答案

我以前遇到过此问题.这可能是您可以在服务器设置中解决的问题.服务器要做的就是获取服务器请求,构建答案,然后再次执行相同的请求时,它将发送与之前构建的响应相同的响应.

I had this problem once before. This is probably 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.

为了轻松避免此问题,我添加了一个额外的请求参数(UID).所以:

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.

这篇关于使用ajax时如何清除缓存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆