Ajax solves the browser's caching problem

Source: Internet
Author: User
Tags add time commit
Today to do the project, almost all submissions are submitted through AJAX, I test found that the data is the same after each commit, debugging can exclude background code problems, so the problem must be out of the foreground. Each time the cache is cleared, a new data is obtained, so the browser caches the problem in the final analysis. Tangled for a long time, finally solved, summed up here.
We all know that Ajax can improve the speed of page loading the main reason is to reduce the loading of duplicate data through Ajax, that is, when loading data into memory, and once the data is loaded, as long as we do not refresh the page, the data will be cached in memory, when we commit URL is consistent with the historical URL, it does not need to be submitted to the server, that is, no need to get data from the server, although this reduces the load on the server to improve the user experience, but we can not get the latest data. To ensure that the information we read is up-to-date, we need to disable his caching capabilities.
There are several solutions to the solution:
1. Add Anyajaxobj.setrequestheader ("If-modified-since", "0") to Ajax before sending the request.
2. Add Anyajaxobj.setrequestheader ("Cache-control", "No-cache") to Ajax before sending the request.
3, add a random number after the URL: "fresh=" + math.random ();.
4. Add time after the URL: "nowtime=" + new Date (). GetTime ();.
5. If you are using jquery, you can $.ajaxsetup ({cache:false}) directly. All of the Ajax on the page will execute this statement without having to save the cache record.
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.