发送异步请求获取数据时,不使用客户端浏览器缓存

来源:互联网 发布:淘宝店推荐 编辑:程序博客网 时间:2024/06/15 04:28

c#中通过XMLHttpRequest请求一般处理程序并根据条件返回结果后不使用客户端缓存的数据,而且每次都从服务器请求数据,

var xhr;         if(window.ActiveXObject){xhr=new ActiveXObject("Microsoft.XMLHTTP");  }         else{xhr=new XMLHttpRequest();}          var url="/handles/ManagerHandler.ashx?action=GetDataJsonList&&PageNum=" + PageNum;        xhr.open("get", url, true);         xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        //禁用客户端缓存,针对IE内核的浏览器        xhr.setRequestHeader("If-Modified-Since","0");         xhr.setRequestHeader("Cache-Control","no-cache");        xhr.onreadystatechange  = function () {            if (xhr.readyState == 4) {                if (xhr.status == 200) {                                      var resDel = xhr.responseText;                    if (resDel == "error") {                        alert("数据操作异常,请重新提交...");                    }                    else{                        var listJsonArr = eval("(" + resDel + ")");                        BindJsonList(listJsonArr); //bind json data to table list                    }                }            }        }        //xhr.send("t=d&TClassId=" + TClassId);           xhr.send(null);

js代码中

xhr.setRequestHeader("If-Modified-Since","0"); xhr.setRequestHeader("Cache-Control","no-cache");

通过此代码段实现不使用客户端缓存的数据,而是直接每次请求都读取服务器端的数据,chrome、Firefox、IE各版本对此段js代码均有效

整理转载自:http://blog.csdn.net/seng3018/article/details/5889894