A brief analysis of the caching problem of XMLHttpRequest

  • 2020-03-30 00:52:23
  • OfStack

Have you ever encountered this in using XMLHttpRequest, the first use is good, refresh is not good, clean out the cache and refresh again.

This is a common problem with xmlhttprequests. Sometimes the result of an XMLHttpRequest request is cached in the same URL, and if the response to the request is different, it can cause some puzzling problems, including the one at the beginning of the article.

Solutions:
It's easy to understand the principle, which is that each request has a different URL and it goes to the server each time instead of using the cache. Here are two common methods:

The first method is timestamped
Var url = BaseURL + "&timeStamp=" + new Date().gettime ();

The second way is to add a random number
Var url = BaseURL + "&r=" + math.random ();
There are probably many other ways, as long as you make sure it goes to the server every time.


Related articles: