Solve jquery load, get method cache data problem

  1. When jquery loads a page, it is only loaded once. There are two solutions.   
  2. 1jQuery provides a method to prevent ajax from using cache,  
  3. <script type="text/javascript" language="javascript">  
  4. $.ajaxSetup({  
  5. cache: false//close AJAX cache   
  6. });  
  7. </script>  
  8. This method must be executed once every time the page is loaded, otherwise it will only be valid for the first time, and other loads will also read the cached page  
  9. 2  The second is to modify the url address loaded by load  
  10. For example, you can add another time parameter to the url  . This method is also used in wap1.0  to avoid reading the cache   .
  11.   
  12. There are two other methods found before, which do not work for this problem:  
  13. 1. Set the cache of html  
  14. <META HTTP-EQUIV="Pragma" CONTENT="no-cache">       
  15. <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">       
  16. <META HTTP-EQUIV="Expires" CONTENT="0">  
  17. 2 , java settings cache  
  18. <%  
  19.     request.setAttribute("decorator""none");  
  20.     response.setHeader("Cache-Control","no-cache"); //HTTP 1.1  
  21.     response.setHeader("Pragma","no-cache"); //HTTP 1.0  
  22.     response.setDateHeader ("Expires"0); //prevents caching at the proxy server  
  23. %><pre name= "code" class = "java" >Jquery $.get method cache problem   
  24.   
  25. In the IE series, the $.get() method will cache the returned result when the url address is fixed, causing unexpected problems. But under Firefox, it will not be cached.  
  26. There are many ways to solve this problem, the most direct is to replace the $.get() method with $.ajax(), and then configure cache: false . I don't like the cumbersome configuration method of $.ajax(), which can be implemented in the simplest way:  
  27. Add a new random parameter after the data of $.get(), such as {data: mydata, stamp: Math.random() }, because the data is different each time, the data returned after the request will not be cached.  
  28. In addition, you can also change $.get() to $.post(), which can also solve this problem.  
  29. The solution once and for all is to set the global parameters, $.ajaxSetup({cache: false }); After this setting, basically all get requests jquery will automatically add additional parameters of the word _1948838, which is similar to the above solution in general .  
  30. E.g:  
  31. $.get("ProvinceListByCountryIDHandler.ashx", { "cid": $drpCountry.val(), "time"new Date().getTime() }, function(data, returnStatus) {})

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326883405&siteId=291194637