Jquery ajax cache solution

Source: Internet
Author: User

Solution:

1. Add a random number to the request link. If jQuery is used, set $. ajaxSetup ({cache: false}) directly });

2. Change type to post, and set a parameter data: 'A = B 'at will (you must set the parameter; otherwise, it will still be cached)

3. To generate different numbers, use the random number Math. random (); or the timestamp + new Date ();

Example

The code is as follows: Copy code

$. Ajax ({
Type: "GET"
Url: 'test.html ',
Cache: false,
DataType: "html ",
Success: function (msg ){
Alert (msg );
    }
});

Or

$. Ajax ({
Type: "GET"
Url: 'test.html? '+ Math. random (),
Cache: false,
DataType: "html ",
Success: function (msg ){
Alert (msg );
    }
});

Later, I found a lot of AJAX GET requests on the Internet, which will be cached for a summary.

1. Add the header on the server ("Cache-Control: no-cache, must-revalidate ");
2. Add anyAjaxObj. setRequestHeader ("If-Modified-Since", "0 & Prime;) before ajax sends a request ;);
3. Add anyAjaxObj. setRequestHeader ("Cache-Control", "no-cache") before ajax sends a request ");
4. Add "? Fresh = "+ Math. random (); // of course, the fresh parameter can be any one here
5. The fourth method is similar to the third method. Add "? Timestamp = "+ new Date (). getTime ();
6. Replace GET with POST: not recommended

A compilation of tips: We have all the above methods for simple tests on ajax cache, which is effective. You can choose one based on your preferences, of course, sometimes we need the cache function, and sometimes you don't need to set it according to the actual situation.

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.