Home > Web Front-end > JS Tutorial > A brief analysis of the caching problem of XMLHttpRequest_javascript skills

A brief analysis of the caching problem of XMLHttpRequest_javascript skills

WBOY
Release: 2016-05-16 17:09:20
Original
1306 people have browsed it

Have you ever encountered this when using XMLHttpRequest? The first time you use it, it works fine, but after refreshing it, it doesn’t work. After clearing the cache and refreshing it, it works again.

This is a problem often encountered when using XMLHttpRequest. Sometimes the results of the XMLHttpRequest request will be cached in the same URL. If the response after your request is different, some inexplicable problems will occur, including of course Haha at the beginning of the article.

Solution:
It’s simple once you know the principle, that is, the URL requested is different each time, so it makes a request to the server every time instead of using cache. Here are two commonly used methods:

The first method is to add a timestamp
var url = BaseURL "&timeStamp=" new Date().getTime();

The second method is to add a random number
var url = BaseURL "&r=" Math.random();
There may be many other methods, as long as you can ensure that It will be OK every time it goes to the server to request.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template