AJAX Caching Issues---disable refresh

Source: Internet
Author: User
Tags php code browser cache

When using jquery Ajax non-flush submission to determine the value in the input box, it is found that the effect is not correct because of the caching problem;

Workaround:

Browser resolution (not recommended):
* Clear the browser cache in Internet Options

* Clear the cache in the F12 developer tool

PHP Code Workaround:
When Ajax T commits, it does not allow the generation of caches;

1, to the request link plus random number, if using jquery, directly set: $.ajaxsetup ({cache:false});

2, change the type to post, and arbitrarily set a parameter data: ' A=b ' (be sure to set the parameters, otherwise it will still be the cache)

3, said to generate a different number, the random number math.random (); or a timestamp + new date ();

Example

The code is as follows

$.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);
}
});

A lot of Ajax get requests that were found on the web were then summed up by the caching workaround.

1, add header on the service side ("Cache-control:no-cache, must-revalidate");
2, add Anyajaxobj.setrequestheader ("If-modified-since", "0″") before Ajax sends the request;
3, add Anyajaxobj.setrequestheader ("Cache-control", "No-cache") to Ajax before sending the request;
4, after the URL parameter of Ajax add "? fresh=" + math.random ();//Of course, here the parameters fresh can be arbitrarily taken
5, the fourth method and a third similar, after the URL parameter with "? timestamp=" + N EW Date (). GetTime ();
6. Replace get with post: not recommended

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.