jquery ajax caching Problem Solution Summary

Today, doing an ajax function started to use the data submitted by the way has been found to get the data submitted are the same, the data returned is also not a long time to refresh, and I know that this is a problem ajax cache, later on the Internet compiled some ajax caching problem solving, Here to share with you. Solution: 1

 
Today, doing an ajax function started to use the data submitted by the way has been found to get the data submitted are the same, the data returned is also not a long time to refresh, and I know that this is a problem ajax cache, later on the Internet compiled some ajax caching problem solving, Here to share with you.
 

Solution:

1, a random number added to the link request, if using the jQuery, disposed directly: $ .ajaxSetup ({cache: false});

2, POST change the type, and a random parameter setting data: 'a = b' (the parameters must be set, otherwise still be cache)

3, talk generated different number, to use a random number Math.random (); or or timestamp + new Date ();

example

 code show as below  

$.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 on the Internet to find a lot of AJAX GET request will be sum up Cache Solution

1, plus the service side header ( "the Cache-Control: Cache-NO, MUST-revalidate");
2, before adding the transmission request ajax anyAjaxObj.setRequestHeader ( "the If-Modified-Since", "0");
. 3 , before adding the transmission request ajax anyAjaxObj.setRequestHeader ( "the Cache-Control", "Cache-NO");
. 4, after adding the URL parameter Ajax "fresh =?" + Math.random ( ); // course here fresh parameter can take any of the
5, similar to the third and fourth method, after the new new URL parameter plus a Date + () the getTime (); "timestamp =?."
. 6, the pOST alternative gET: not recommended

A poly Xiao Bian Tip You: ajax caching problem on our above methods have a simple test is valid according to their own people prefer it, of course, sometimes we need caching feature sometimes we may not need to be set according to the actual situation.

Guess you like

Origin www.cnblogs.com/zhuyeshen/p/11599254.html