Home > Web Front-end > JS Tutorial > How jquery handles ajax caching issues

How jquery handles ajax caching issues

coldplay.xixi
Release: 2020-11-17 11:36:11
Original
2049 people have browsed it

How jquery handles ajax caching issues: 1. Directly set [$.ajaxSetup({cache: false})]; 2. Change the type to post and set a parameter [data: 'a= b'].

How jquery handles ajax caching issues

Recommended: "jquery video tutorial"

How jquery handles ajax caching problems:

1. Add a random number to the request link. If you are using jQuery, set it directly: $.ajaxSetup({cache: false});

2. Change the type to post, and set any parameter data: 'a=b' (Be sure to set the parameters, otherwise it will still be cached)

3. Talk about the different generation For the number, use random numbersMath.random();ortimestamp new Date();

Example

The code is as follows

$.ajax({
    type:"GET"
    url:'test.html',
    cache:false,
    dataType:"html",
    success:function(msg){
        alert(msg);
    }
});
Copy after login

or

$.ajax({
    type:"GET"
    url:'test.html?'+Math.random(),
    cache:false,
    dataType:"html",
    success:function(msg){
        alert(msg);
    }
});
Copy after login

Later I found many AJAX GET requests on the Internet that will be cached. Let me summarize the solution

1. Add header(" Cache-Control: no-cache, must-revalidate”);

2. Add anyAjaxObj.setRequestHeader(“If-Modified-Since”,”0″ before sending the ajax request );

3. Add anyAjaxObj.setRequestHeader(“Cache-Control”,”no-cache”);

4 before sending the ajax request , add "?fresh=" after the Ajax URL parameter Math.random(); //Of course, the fresh parameter here can be chosen arbitrarily

5. The fourth method is similar to the third method, in the URL Add "?timestamp=" new Date().getTime();

6. Use POST instead of GET: Not recommended

Related free learning recommendations: JavaScript(Video)

The above is the detailed content of How jquery handles ajax caching issues. For more information, please follow other related articles on the PHP Chinese website!

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