jquery 缓存有关问题

jquery 缓存问题

转载:http://halk.iteye.com/blog/592306

现象: 页面使用jQuery的Ajax方式更改内容,提交保存至数据库后使用jQuery.getJSON方式重新读取数据,页面没有更新,重新登录系统后页面才显示更改后的数据。
原因: IE会缓存jQuery的get请求,导致不能查询到新的数据
解决方法
1、在请求URL后添加随机数,例如:

Js代码 jquery 缓存有关问题 jquery 缓存有关问题jquery 缓存有关问题
  1. var url ="/operaterMultiAction.do?randomNum="+Math.random();  
var url ="/operaterMultiAction.do?randomNum="+Math.random();

2、使用POST方式定义JSON调用,如:

Js代码 jquery 缓存有关问题 jquery 缓存有关问题jquery 缓存有关问题
  1. $.postJSON = function(url,data,callback){   
  2.         $.post(url,data,callback,"json");   
  3. }  
$.postJSON = function(url,data,callback){
        $.post(url,data,callback,"json");
}

3、使用ajax方法,声明缓存为false

Js代码 jquery 缓存有关问题 jquery 缓存有关问题jquery 缓存有关问题
  1. $.ajax({   
  2.         url:"/operaterMultiAction.do",   
  3.         dataType:"json",   
  4.         cache:false,   
  5.         success:function(data){   
  6.         }   
  7.     });