Two ways to suppress caching in Ajax

Source: Internet
Author: User

When using Ajax to perform an asynchronous refresh of a part of a page, after grabbing the packet, you can find that the problem is that if the contents of the form are unchanged, it does not make a request to the server, but instead caches the requests sent to the browser, thus extracting the content, which is an internal default mechanism of the browser, but sometimes We do not need to do this, need is every time to send a request to the server, into the learning, there are two ways to implement the prohibition of caching:

1: Send a request at the same time send a dynamic data, so-called dynamic, is always changing, such as date (), so that each request is "not the same", thereby indirectly implementing the forbidden cache, the following is an example:

var url = "02.php?mytime =" +new Date () + "&usename=" +$ ("Usename"). Value;

2: Is directly on the return page of the data to add a non-cached two statements, this is the data are all forbidden to cache:

Header ("Content-type:text/xml; Charset=utf-8 ");
Header ("Cache-control:no-cache");

Note: The first sentence is not to prohibit caching, but to prompt the UTF-8 to the encoding method to return data, and generally need to combine the two, and note that the above quotation marks can not be placed in the middle, at least in my editor is this, not the reason, So personal advice is the safest place on the left.


Here is a copy of my example code (contains two PHP files, one for sending, one for receiving)

Send page:

 

Receive page:

<?php
	/** The following is the second way that the browser disables caching
	, you must pay attention to the location of the quotation marks, find the quotation marks in the middle of the error *
	/header ("Content-type:text/xml; Charset=utf-8 ");
	Header ("Cache-control:no-cache");
	$usename = $_post[' usename ');      Line Line 3 receives
	/*echo "username is". $usename; *///read the cache is certain
	if ($usename = = "Xuning") {
		echo ' username already exists ';
	} else{
		echo "User name can be used";
	}
? >



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.