This paper analyzes the solution of IE caching problem in Ajax invocation. Share to everyone for your reference, specific as follows:
The problem found in the AJAX request call: The background request is a simple. aspx file, and this page does not take into account the impact of the cache, the use of Ajax debugging found that sometimes the background code directly returned results, so it is estimated to be affected by the browser cache. Search on the Internet, is really the problem of caching: "ie if the URL submitted by the XMLHttpRequest and the same as the history of the use of caching, do not submit to the server side." Therefore, you cannot get the data you just submitted or the new data .
There are roughly the following solutions:
1, only improve the server side
(1) The background is a simple. aspx file, directly plus
Copy Code code as follows:
<%@ OutPutCache location= "None"%>
Can.
(2) But if the background is a. ashx file, you typically modify the cache settings directly in the class.
Copy Code code as follows:
Context. Response.Cache.SetCacheability (Httpcacheability.nocache);
2, only improve the client
(1) Change the request address by adding random number or time stamp
A, plus random number:
Copy Code code as follows:
var url= ' ajaxoperations.aspx?rdm= ' +math.random () + ' &action= ' +op;
b, plus time stamp:
Copy Code code as follows:
var url= ' ajaxoperations.aspx?dtstamp= ' +new Date (). GetTime () + ' &action= ' +op;
This solution, only need to add a time or random number as a parameter, the server side does not need any changes to achieve the purpose.
(2) The foreground asynchronous call sets the properties of the XMLHttpRequest object
Add the XMLHttpRequest before sending the request
Copy Code code as follows:
Xmlhttprequest.setrequestheader ("If-modified-since", "0")
Personally think this is the "right path." Because you can't be clear about which Ajax requests are needed or not cached (address the performance bottlenecks of the site, with cached Ajax in the majority), so each time you send a request to confirm whether to cache, relative to 2 of the article (1) obviously less than a URL parameter, and the server side (improved Method 1) does not need to change the settings, but for the encapsulated AJAX libraries, you may not be able to use the XMLHttpRequest object directly outside, the usual setting syntax may be ("if-modified-since", "0") Pass as parameter: Ajaxobj.sendpost (other parameter list,.... "If-modified-since", "0"); (POST method)
or ajaxobj.sendget (other parameter list,.... "If-modified-since", "0"); (Get Way)
I hope this article will help you with Ajax programming.