天天看点

神奇的IE9,Ajax请求缓存问题

神奇的IE9,Ajax请求缓存问题

如图所示:点击“发表评论”,发表一条商品评论。

相关的代码如下:

//评价之前判断是否登录,如果已登录跳转到评价页面,如果没登陆提示先登录
function judgeUserLogin(itemId,itemGroupId){
	$.ajax({
        url: shop_Path+"front/consult/judgeLogin.htm",  
        type: 'POST',
        dataType: 'JSON',  
        timeout: 5000,  
        error: function() { },  
        success: function(data) { 
	        if (data.result) {
	        	jConfirm("请登录后再对商品进行评价!", "提示信息",function(r){
	        		if (r) {
	        			window.location.href = occweb_Path+"login.htm?redirect="+location.href;
	        		}
	      		  });
	        } else {
	        	addItemAppraisal(itemId,itemGroupId);
	        }
        }
  });
}
           
//新增评价
function addItemAppraisal(itemId,itemGroupId){
	var comment = $("#comment").val();
	$.ajax({
        url: shop_Path+"front/addItemAppraisal.htm",
        type: 'POST',
        dataType: 'JSON',
        data:{itemId:itemId,
        	  itemGroupId:itemGroupId,
        	  comment:comment},
        success: function(data) { 
	        if (data.success) {
	        	$("#comment").val("");
	        	$("#comment").focus();
	            $("#itemAppraisal_container").gotoPage(1,$("#itemGroupId").val());
	        } else {
	        	jAlert("评论失败!","提示信息");
	        }
        }
    });
}
           
//评价的ajax分页
	$.extend($.fn,{
        // 加载 内容
        gotoPage:function(index,itemGroupId){
          var _this=this;
          var message = prepareErrorMessage("正在加载...","<img src='common/images/loading.gif'/>");
		  $(_this).html(message);
          $.ajaxSetup({
        	  <strong><span style="color:#ff0000;">cache:false,</span></strong>
        	  timeout: 30000,//30秒超时时间
        	  error: function (xhr, status, e){
	    		  if(status=="timeout"){
	    			  var message = prepareErrorMessage("页面链接超时,请重新刷新","<a href='javascript:void(0)' οnclick='window.location.reload();'>刷新</a>");
	        		  $(_this).html(message);
	    		  }
        	  }
          });
          $.get(shop_Path+"front/getItemAppraisal.htm",{page:index,itemGroupId:itemGroupId},function($html){
        	if($html.msg){
        		var message = prepareErrorMessage($html.msg,"");
        		$(_this).html(message);
        		return false;
        	}
        	$(_this).html($html);
          });
        }
    });
           

以上代码在没有加入红色部分之前,在IE8、10,firefox,google等浏览器都是okey的,但是唯独在IE9下不行,把IE9关了,又重新打开,就能看到之前新增的记录,这时我们公司的前端大牛,过来看了一眼,缓存问题,然后加入了上面的那段,就可以了。

问题认证:因为IE9是

神奇的IE9,Ajax请求缓存问题
神奇的IE9,Ajax请求缓存问题
神奇的IE9,Ajax请求缓存问题

,设置为“自动”时,IE不会自动清除浏览器缓存,但是像IE8、10等其他浏览器为“每次访问网页时(E)”,就是每次请求都检测最新的,所以就不会有缓存问题。

继续阅读