Solution to ie cache problems in ajax calls,

Source: Internet
Author: User

Solution to ie cache problems in ajax calls,

This article describes how to solve the ie cache problem in ajax calls. We will share this with you for your reference. The details are as follows:

Problems found during ajax request calling: the background request is simple. the cache effect has not been taken into account on the aspx file. When ajax debugging is used, it is found that sometimes the results are directly returned without the background code, 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, directly addedCopy codeThe Code is as follows: <% @ OutPutCache Location = "None" %>.

(2) but if the background is a. ashx file, the cache settings are usually directly modified in the class.
Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: var url = 'ajaxoperations. aspx? Rdm = '+ Math. random () +' & action = '+ op;
B. timestamp:Copy codeThe Code is as follows: 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) Set attributes of the XMLHttpRequest object in asynchronous call at the foreground

Before XMLHttpRequest sends a request, addCopy codeThe Code is as follows: 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)

I hope this article will help you with ajax programming.

Articles you may be interested in:
  • Ajax + php controls all background function calls
  • JQuery Ajax call to the WCF Service
  • Summary of common methods for jQuery to call ajax requests
  • Use ajax technology to call Sina stock real-time data without refreshing
  • JQuery ajax calls a WCF Service instance
  • Regular ajax call every 5 seconds
  • Iframe ajax call sample code
  • How to remotely call ajax methods in jquery through JSONP
  • JavaScript calls ajax to obtain the implementation code of Text File Content
  • Iframe ajax call example
  • Jquery. Ajax () method to call Asp. Net background method Parsing
  • Simple ajax call example

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.