Methods to prevent Ajax caching in (ASP/PHP/JSP/HTML/JS) collection _ajax related

Source: Internet
Author: User

Ajax caching is good, but also bad, caching can sometimes lead to misoperation, affect the user experience, if your Web project does not require AJAX caching capabilities, you can use the following methods to prevent Ajax caching.

I. Disable AJAX caching in asp:

' At the beginning of the ASP page

Response.expires=0

Response.AddHeader ("Pragma", "No-cache")

Response.AddHeader ("Cache-control", " No-cache, Must-revalidate ")

Second, in PHP to prevent AJAX caching:

Placed at the beginning of PHP page

header ("Expires:thu, 1970 00:00:01 GMT");

Header ("Cache-control:no-cache, must-revalidate");

Header ("Pragma:no-cache");

Disable AJAX caching in JSP:

Placed at the beginning of the JSP page

response.addheader ("Cache-control", "No-cache");

Response.AddHeader ("Expires", "Thu, 1970 00:00:01 GMT");

forcing updates by adding random characters to a webpage :

var url = ' http://url/';

url = '? temp= ' + New Date (). GetTime ();

url = '? temp= ' + math.random ();

If static HTML, you can add HTTP headers headers to prevent caching, such as:

<meta http-equiv= "Pragma" content= "No-cache"/> <meta http-equiv= "Cache-control" content= "No-cache"

, Must-revalidate "/> <meta http-equiv=" Expires "content=" Thu 1970

GMT "00:00:01/>

http-equiv= "Expires" content= "0"/>

Six, you can prevent Ajax caching by adding the following code before XMLHttpRequest send the request:

Xmlhttprequest.setrequestheader ("If-modified-since", "0");
Xmlhttprequest.send (NULL);

Seven, jQuery Ajax load ban

When jquery provides a way to prevent Ajax from using caching, adding the following statement to the head's JavaScript file can solve the problem.

$.ajaxsetup ({ 
  cache:false//close Ajax corresponding cache 
});


Summary, but now it's all using jquery ajax. If we don't want the cache to be set directly, we can solve the cache:false of post, get, and so on.

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.