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

How jquery handles ajax caching issues

coldplay.xixi
coldplay.xixiOriginal
2020-11-17 11:36:112043browse

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);
    }
});

or

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

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!

Statement:
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