Ajax front-end solves the problem of caching

The cache problem of Ajax is very serious. When Ajax is used to read the page for the second time, even if the content of the page is changed, the content of the page requested for the first time is read.

The correct solution is to handle it in the background, not much to say 

There are two front-end processing methods 

The first is random number

xhr.open("get", "text.txt?type=" + Math.random(), true);

 

The second is timestamp

 xhr.open("get", "text.txt?type=" + Date.parse(new Date), true);

 

Overall request code

 

   <script>
        var btn = document.getElementById("btn");
        var text = document.getElementById("text");
        btn.onclick = function () {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                text.innerHTML = xhr.responseText
            }
            //xhr.open("get", "text.txt?type=" + Math.random(), true);
            xhr.open("get", "text.txt?type=" + Date.parse(new Date), true);
            xhr.send(null)
        }
    </script>

 

Guess you like

Origin blog.csdn.net/weixin_41040445/article/details/114886267