Cache problem with ajax(text files)

Asked

Viewed 59 times

1

I made the ajax connection to introduce a file. txt in a div, but I modified the text and saved but the text is not changed (as if I hadn’t saved it)

If you want to see the site where the text is (biology->Food Chain): http://estudos.epizy.com/

The "F12" of the page: https://prnt.sc/libg1i What is saved (left) and what appears (right):https://prnt.sc/lic93o

<script>
function corpo(titulo, texto) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        document.getElementById("titulo").innerHTML = titulo;
        document.getElementById("corpo").innerHTML =
        this.responseText;
    }
  };
  xhttp.open("GET", texto, true);
  xhttp.send();
}

1 answer

2


This is cache. Try the following.

On the line.

xhttp.open("GET", texto, true);

Exchange for:

xhttp.open("GET", texto + '?_now_=' + ((new Date()).getTime()), true);

Add the current time in the request, make every request a new one for the browser and then the cache is ignored.

  • It worked. Thank you very much - ) I didn’t know that in ajax I also had cache problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.