Projects sometimes use some Ajax effects. Because it is relatively simple, there is no such thing as ajax.net, And the handwritten code is implemented. The next day, someone reported an error to me, saying that only the value read for the first time is normal, and the subsequent values are not normal. After debugging, this problem exists, it is found that the problem is caused by Ajax cache. There are several solutions: What are the casinos in Macao?
- Add the header ("cache-control: No-cache, must-revalidate") on the server (such as in PHP)
- Add anyajaxobj. setRequestHeader ("If-modified-since", "0") before Ajax sends a request ");
- Add anyajaxobj. setRequestHeader ("cache-control", "No-Cache") before sending a request through Ajax ");
- Add "? Fresh = "+ math. Random (); // Of course, the fresh parameter can be any one here
- The fifth method is similar to the fourth method. Add "? Timestamp = "+ new date (). gettime ();
- Replacing get with post: Not recommended
Add a random number:
xmlHttp.open("GET", "ajax.asp?now=" + new Date().getTime(), true);
On the ASP page to be obtained asynchronously, write a piece of code to prohibit Caching:
Response.Buffer =TrueResponse.ExpiresAbsolute =Now() - 1Response.Expires=0Response.CacheControl="no-cache"
Add XMLHTTP. setRequestHeader ("If-modified-since", "0") before Ajax sends a request.
xmlHTTP.open("get", URL, true); xmlHTTP.onreadystatechange = callHTML; xmlHTTP.setRequestHeader("If-Modified-Since","0"); xmlHTTP.send();
The Ajax cache is maintained by the browser. For a URL sent to the server, Ajax only interacts with the server during the first request. In subsequent requests, Ajax no longer submits requests to the server, instead, extract data directly from the cache.
In some cases, we need to obtain the updated data from the server every time. The idea is to make the URLs of each request different without affecting the normal application: Add random content after the URL.
url=url+"&"+Math.random();
- The URLs of each request are different (the Ajax cache does not work)
- Does not affect normal applications (most basic)
Disable caching in JSP:
response.addHeader("Cache-Control", "no-cache");response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
We all know that the main reason why AJAX can speed up page loading is that it reduces the loading of duplicate data through Ajax and truly achieves on-demand acquisition. In this case, when writing an Ajax program, we may send it to the west and cache it again on the client side to further increase the data loading speed. That is, when loading data, the data is cached in the browser memory. Once the data is loaded, the data is always cached in the memory as long as the page is not refreshed, when you view the data again, you do not need to obtain the data from the server, which greatly reduces the server load and improves the user experience.
Summary of comprehensive Ajax cache Problems