Solve the Problem of IE caching in Ajax calls

Source: Internet
Author: User
Problems found during Ajax request calling: the background request is simple. this page has not considered the impact of caching. When using Ajax for debugging, it is found that sometimes the background is not used. Code The result is returned directly, so it is estimated that it is affected by the browser cache. I searched the internet and it was a cache problem:" In IE, if the URL submitted by XMLHttpRequest is the same as that in history, the cache is used, and the request is not submitted to the server. Therefore, you cannot obtain the submitted data or new data. ".
There are several solutions:
1. Only improve the server
(1) The background is a simple. aspx file. Simply add <% @ outputcache location = "NONE" %>.
(2) but if the background is a. ashx file, the cache settings are usually directly modified in the class.
Context. response. cache. setcacheability (httpcacheability. nocache );
2. Only improve the client
(1) Change the request address by adding a random number or Timestamp
A. Add a random number: var url = 'ajaxoperations. aspx? RDM = '+ math. Random () +' & Action = '+ OP;
B. Add a timestamp: var url = 'ajaxoperations. aspx? Dtstamp = '+ new date (). gettime () +' & Action = '+ OP;
In this solution, you only need to add a time or random number as the parameter, and the server can achieve the goal without any changes.
(2) Foreground asynchronous call settings XMLHttpRequest Object Attributes
Before XMLHttpRequest sends a request, add XMLHttpRequest. setRequestHeader ("If-modified-since", "0 ") I personally think this is the right way ". Because you cannot determine which Ajax requests need or do not need to be cached (solve the performance bottleneck of the website and use mostly cached Ajax requests ), therefore, each time you send a request, check whether the request is cached. this parameter is obviously missing from entry (1) of article 2, and the server side (Improvement Method 1) you do not need to change the settings, but for the encapsulated Ajax library, you may no longer be able to directly use the XMLHTTPRequest object outside, the general setting syntax may be to pass ("If-modified-since", "0") as a parameter: ajaxobj. sendpost (other parameter list ,.... "If-modified-since", "0"); (POST method)
Or ajaxobj. sendget (other parameter list,... "If-modified-since", "0"); (get method)
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.