IE会对AJAX请求结果进行一个缓存。导致了一个问题,在下一次进行请求时,看的是本地的缓存而不是服务器的最新数据。影响时效性强的结果
IE缓存问题.html
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
const btn = document.querySelector("button");
const result = document.querySelector("#result");
btn.addEventListener('click', function(){
const xhr = new XMLHttpRequest();
xhr.open("GET","http://127.0.0.1:8000/ie");//****
xhr.send();
xhr.onreadystatechange = function() {
if(xhr.readyState === 4){
if(xhr.status >=200 && xhr.status < 300){
result.innerHTML = xhr.response;
}
}
}
})
</script>
</body>
server.js:add
//设置一个专门针对IE缓存问题的
app.get('/ie',(request, response)=>{//***
//设置响应头 设置允许跨域
response.setHeader("Access-Control-Allow-Origin","*");
//设置响应体
response.send("Hello IE-3");
})
此时更改服务器response.send("Hello IE-change");
IE浏览器的返回结果不变。
更改htmlxhr.open("GET","http://127.0.0.1:8000/ie?t="+Date.now());
Date.now()获取当前时间戳
两次请求服务时,时间戳肯定是不同的,IE浏览器就会发现不同,从而不会去从缓存中寻找
加号是将前面的字符串和后面的时间戳变量拼接起来。
此时就会改变了。
当然实际工作时,这个问题是比较 少遇见的。
ie即将退出舞台,我们也会面向未来编程