Ajax cache processing solution for get request

Time:2021-3-23

This article describes the Ajax get request cache processing solution. Share with you for your reference. The specific analysis is as follows:

Many times, when Ajax get method is called, the correct data can’t be obtained in time due to cache. Here’s how to solve this problem

1. Add a random number after the URL, such as:

Copy codeThe code is as follows:
http://www.test.com?a=a&b=b&r=Math.random();

2. Add a timestamp after the URL:

Copy codeThe code is as follows:
var t = new Date().getTime();
http://www.test.com?a=a&b=b&t=t

3. Use the setrequestheader function of XMLHttpRequest object to set the value of if modified since to 0, such as

Copy codeThe code is as follows:
xmlHttp.setRequestHeader(‘If-Modified-Since’, 0);

Here we need to understand the meaning of if modified since

4. Use the header function of PHP

Copy codeThe code is as follows:
header(‘Cache-Control:no-cache, must-revalidate’);

Set cache not to be used

I hope this article is helpful to your Ajax programming.