Solution to ie cache problem in Ajax call

Time:2020-11-9

This paper analyzes the solution of ie cache problem in Ajax call. For your reference, the details are as follows:

The problem found in the process of Ajax request call: the background request is a simple. ASPX file, and this page does not consider the impact of cache. When debugging with Ajax, it is found that sometimes the result is returned directly without going through the background code, so it is estimated that it is affected by the browser cache. As expected, the problem of searching the cache is as follows:“In ie, if the URL submitted by XMLHttpRequest is the same as the history, cache is used, and it is not submitted to the server at all. Therefore, the newly submitted data or new data cannot be retrieved”。

The solutions are as follows:

1. Improve server side only

(1) Background is a simple. ASPX file, directly add

Copy codeThe code is as follows:
<%@ OutPutCache Location=”None”%>

That’s fine.

(2) However, if the background is an. Ashx file, the cache settings are usually modified directly in the class.

Copy codeThe code is as follows:
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

2. Improve client only

(1) Change request address by adding random number or adding time stamp

a. Add random number:

Copy codeThe code is as follows:
var url=’AjaxOperations.aspx?rdm=’+Math.random()+’&action=’+op;

b. Time stamp:

Copy codeThe code is as follows:
var url=’AjaxOperations.aspx?dtStamp=’+new Date().getTime()+’&action=’+op;

This solution only needs to add a time or random number as a parameter, and the server does not need any changes to achieve the purpose.

(2) The foreground asynchronous call sets the properties of the XMLHttpRequest object

Add before XMLHttpRequest to send the request

Copy codeThe code is as follows:
XMLHttpRequest.setRequestHeader(“If-Modified-Since”,”0″)

I think this is the right way. Because you can’t determine which Ajax requests need or do not need to be cached (to solve the performance bottleneck of the website, most of the Ajax with cache is used), so each time you send a request, you should confirm whether you want to cache it. Compared with item (1) in 2, it is obviously less to write a URL parameter, and the server side (improvement method 1) does not need to change the settings, but for the already encapsulated Good Ajax library, you may not be able to use XMLHttpRequest object directly outside. The usual setting syntax may be to pass (“if modified since”, “0”) as a parameter: ajaxObj.sendPost (list of other parameters,…. “if modified since”, “0”); (post mode)

Or ajaxObj.sendGet (list of other parameters,…. “if modified since”, “0”); (get method)

I hope this article will be helpful to everyone’s Ajax programming.