浏览器缓存问题、浏览器数据刷新无效
众所周知,浏览器具有缓存功能,这样会导致我们的数据有时刷新无效;但是,我们有时不希望浏览器缓存,我们希望获取的数据总是最新的数据,这时,我们可以通过ajas的方法实现,拼接时间参数,即每次访问都是不一样的网址,从而达到没有缓存的效果;
另外,ie浏览器在使用ajax的解析路径的方法与其他浏览器不同,需要使用encodeURI()函数 对地址进行统一编码。
//IE下get方式传中文参数乱码解决方法
// 乱码原因:浏览器在传递url的时候,会使用自己的编码格式对地址进行编码,如果浏览器所使用编码与服务器采用编码不一致,服务器接收到的参数就会出现乱码。在firefox,chrome下正常,ie下会出现乱码。
// 解决方法:使用js encodeURI 对地址进行统一编码,
demo如下:
var xmlhttp;
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){
//php页面返回的数据为data
data=xmlhttp.responseText;
}
}
if(isIE()){ //拼接时间参数,每次网址就不一样啦
xmlhttp.open("GET",encodeURI("php/get_eqpt_name.php?eqpt_input="+eqpt_input+"&time="+new Date().getTime()),true);
}
else{
xmlhttp.open("GET","php/get_eqpt_name.php?eqpt_input="+eqpt_input+"&time="+new Date().getTime(),true);
}
xmlhttp.send();
//判断当前浏览器是否为ie浏览器
function isIE() { //ie?
if (!!window.ActiveXObject || "ActiveXObject" in window){
// console.log("is ie")
return true;
}
else{
// console.log("is not ie")
return false;
}
}
本文探讨了浏览器的缓存特性导致的数据刷新无效问题,并提出通过AJAX动态添加时间参数来避免缓存,确保获取最新数据。同时提到了IE浏览器在AJAX解析路径时的特殊性,需要使用encodeURI()函数进行地址编码。

8639

被折叠的 条评论
为什么被折叠?



