Java:Ajax的内容也会被缓存吗?


   
  <script type="text/javascript">
  
$(document).ready(function() {
$("#pb1").progressBar();
});
function ajax() {
$.ajax({
type : 'GET',
url : "servlet/Test",
dataType : 'text',
async : true,
success : function(response) {
alert(response);
$("#pb1").progressBar(response);
},
error : function(xhr, r, e) {
alert(xhr + "," + r + "," + e);
}
});
}
</script>

通过ajax获取后台返回的代码

   
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  
throws ServletException, IOException {
PrintWriter out;
try {
out = response.getWriter();
out.write("75");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

后台是一个简单的Servlet的doGet方法

现在我部署项目,启动tomcat,第一次请求时正常,返回的75,此时我将75改成40,然后再次请求,还是返回的75...然后我在想这东西是不是会被浏览器缓存呢?=。=于是我从Chrome换成了Firefox,还是返回75,真是活见鬼了....Ajax会一直返回第一次返回的值吗?

java Ajax

HandJob 10 years, 1 month ago

一个简单的方法是加一个参数(例如当前时间),让每次请求的URI不同,浏览器就不会缓存,只需要改客户端,不用改服务器。

另外的方法就是用一些header(Cache-Control: no-cache)告诉浏览器不要缓存。

另外, Chrome的开发者工具可以很方便地清空和禁用缓存,调试就不用换到FF了。

vvvlmks answered 10 years, 1 month ago

Your Answer