Solve the problem that IE cached data is not updated after JQuery sends an Ajax request

Source: Internet
Author: User

Today, when I requested data from the backend server without refreshing the ajax page, I encountered Ajax cache in IE. Both FF and Chrome are normal, because I didn't know that IE had this bad problem at the beginning, finally solve the problem.

By default, IE caches the Ajax request results for the request address. In other words, before the cache expires, multiple Ajax requests initiated for the same address will only be sent to the server for the first time. In some cases, this default caching mechanism is not what we want (such as obtaining real-time data ).


When Ajax is used in IE to request a page, the previous result is usually returned due to cache, causing confusion (for example, multiple requests but no response ).

Problem reproduction:

Window. setInterval (realtimeHandle, 3000 tempCounter = (dbCounter! = TempCounter & tempCounter! = "Too many threads have been written, too many threads have been written, too many threads have been written! Now Count = "+ =" json"

[That is, when the get method is used, the IE browser retrieves data from the cache because the sending parameters and addresses are consistent, instead of requesting the server, the post method does not cause this problem because of different parameters.]

But this does not happen in FF and Chrome.

 

Solution:
IChange

Internet Options -- browsing history -- settings -- the option of Temporary Internet Files is changed to the option that can be used to access the webpage each time.
 

Add a random function after the page of the AJAX request. For example, we can use a random time function. Add it after the URL sent by javascript.

Random Function Code example:

 randomString = "?r=" (  i = 0; i < 20; i+++= Math.floor(Math.random() * 10

For example:
URL + "&" + "t =" + Math. random (); // or new Date ();
"? Timestamp = "+ new Date (). getTime ();

In addition, we can also set it as follows:
$. AjaxSetup ({

})
In fact, this mechanism of jQuery is also implemented by adding different query string suffixes to the request address.

 

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.