How to disable ajax caching (ASP/PHP/JSP/html/js)

Source: Internet
Author: User

Ajax cache is good, but it also has some disadvantages. Caching sometimes leads to misoperations and affects user experience. If your WEB project does not require ajax caching, you can disable ajax caching in the following ways.

1. Disable ajax caching in ASP:

'At the beginning of ASP web page

The code is as follows: Copy code

Response. expires = 0

Response. addHeader ("pragma", "no-cache ")

Response. addHeader ("Cache-Control", "no-cache, must-revalidate ")

2. Disable Ajax caching in PHP:

The code is as follows: Copy code

// Place it at the beginning of the PHP webpage

Header ("Expires: Thu, 01 Jan 1970 00:00:01 GMT ");

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

Header ("Pragma: no-cache ");

3. Disable ajax caching in JSp:

 

The code is as follows: Copy code

// Place it at the beginning of the JSP page

Response. addHeader ("Cache-Control", "no-cache ");

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

4. Force update by adding random characters to the webpage:

The code is as follows: Copy code

Var url = 'http: // url /';

Url + = '? Temp = '+ new Date (). getTime ();

Url + = '? Temp = '+ Math. random ();

5. If it is static HTML, you can add an HTTP headers header to disable caching. For example:

The code is as follows: Copy code

<Meta http-equiv = "pragma" content = "no-cache"/>

<Meta http-equiv = "Cache-Control" content = "no-cache, must-revalidate"/>

<Meta http-equiv = "expires" content = "Thu, 01 Jan 1970 00:00:01 GMT"/>

<Meta http-equiv = "expires" content = "0"/>

6. You can add the following code to prohibit ajax caching before XMLHttpRequest sends a request:

The code is as follows: Copy code

XMLHttpRequest. setRequestHeader ("If-Modified-Since", "0 ");
XMLHttpRequest. send (null );

VII. jQuery ajax Load disabled

JQuery provides a method to prevent ajax from using the cache. You can solve the problem by adding the following statements to the javascript file of the head.

The code is as follows: Copy code

$. AjaxSetup ({
Cache: false // disable the corresponding AJAX cache
});


Summary, but now we use jquery + ajax/"target =" _ blank "> jquery ajax. If we don't want to cache, we can directly set cache: false to solve post, get and other data submission methods.

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.