Solution:
1. Add a random number to the request link. If jQuery is used, set $. ajaxSetup ({cache: false}) directly });
2. Change type to post, and set a parameter data: 'A = B 'at will (you must set the parameter; otherwise, it will still be cached)
3. To generate different numbers, use the random number Math. random (); or the timestamp + new Date ();
Example
The code is as follows: |
Copy code |
$. Ajax ({ Type: "GET" Url: 'test.html ', Cache: false, DataType: "html ", Success: function (msg ){ Alert (msg ); } }); Or $. Ajax ({ Type: "GET" Url: 'test.html? '+ Math. random (), Cache: false, DataType: "html ", Success: function (msg ){ Alert (msg ); } }); |
Later, I found a lot of AJAX GET requests on the Internet, which will be cached for a summary.
1. Add the header on the server ("Cache-Control: no-cache, must-revalidate ");
2. Add anyAjaxObj. setRequestHeader ("If-Modified-Since", "0 & Prime;) before ajax sends a request ;);
3. Add anyAjaxObj. setRequestHeader ("Cache-Control", "no-cache") before ajax sends a request ");
4. Add "? Fresh = "+ Math. random (); // of course, the fresh parameter can be any one here
5. The fourth method is similar to the third method. Add "? Timestamp = "+ new Date (). getTime ();
6. Replace GET with POST: not recommended
A compilation of tips: We have all the above methods for simple tests on ajax cache, which is effective. You can choose one based on your preferences, of course, sometimes we need the cache function, and sometimes you don't need to set it according to the actual situation.