Ajax--低版本IE浏览器的缓存问题及解决方法

低版本IE浏览器的缓存问题:
在低版本的IE浏览器中,Ajax请求有严重的缓存问题,即在请求地址不发生变化的情况下,
只有第一次请求会真正发送到服务器端,后续的请求都会从浏览器的缓存中获取结果,即使
服务器端的数据更新了,客户端依然拿到的是缓存中的旧数据。

解决方案:

在请求地址的后面加请求参数,保证每一次请求参数的值不相同。
xhr.open('get','http://localhost"3000/cache?t='+Math.random())

.html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8">
 5     <title>07Ajax错误处理.html</title>
 6 </head>
 7 <body>
 8     <button id='btn'>发送Ajax请求</button>
 9     <script type="text/javascript">
10         var btn=document.getElementById('btn');
11         btn.onclick=function(){
12             var xhr=new XMLHttpRequest();
13             xhr.open('get','http://localhost:3000/cache?t='+Math.random());
14             xhr.send();
15             xhr.onreadystatechange=function(){
16                 if(xhr.readyState==4 && xhr.status==200){
17                     alert(xhr.responseText)
18                 }
19             }
20             
21         }
22     </script>
23 </body>
24 </html>

猜你喜欢

转载自www.cnblogs.com/technicist/p/12742338.html