IE会对AJAX请求结果进行一个缓存。导致了一个问题,在下一次进行请求时,看的是本地的缓存而不是服务器的最新数据。影响时效性强的结果

    IE缓存问题.html

    1. <body>
    2. <button>点击发送请求</button>
    3. <div id="result"></div>
    4. <script>
    5. const btn = document.querySelector("button");
    6. const result = document.querySelector("#result");
    7. btn.addEventListener('click', function(){
    8. const xhr = new XMLHttpRequest();
    9. xhr.open("GET","http://127.0.0.1:8000/ie");//****
    10. xhr.send();
    11. xhr.onreadystatechange = function() {
    12. if(xhr.readyState === 4){
    13. if(xhr.status >=200 && xhr.status < 300){
    14. result.innerHTML = xhr.response;
    15. }
    16. }
    17. }
    18. })
    19. </script>
    20. </body>

    server.js:add

    1. //设置一个专门针对IE缓存问题的
    2. app.get('/ie',(request, response)=>{//***
    3. //设置响应头 设置允许跨域
    4. response.setHeader("Access-Control-Allow-Origin","*");
    5. //设置响应体
    6. response.send("Hello IE-3");
    7. })

    image.png
    此时更改服务器response.send("Hello IE-change");IE浏览器的返回结果不变。
    更改html
    xhr.open("GET","http://127.0.0.1:8000/ie?t="+Date.now());Date.now()获取当前时间戳
    两次请求服务时,时间戳肯定是不同的,IE浏览器就会发现不同,从而不会去从缓存中寻找

    加号是将前面的字符串和后面的时间戳变量拼接起来。

    image.png
    此时就会改变了。

    当然实际工作时,这个问题是比较 少遇见的。

    ie即将退出舞台,我们也会面向未来编程