How to disable caching and coding instance code when writing Ajax in PHP?

Source: Internet
Author: User
Encoding Problems . By default, the use of UTF-8, if you find that the object can not be found, may be JS input in the Chinese, while the JS encoding format may be gb2312, open JS available notepad, save as UTF-8 format documents.

Data obtained through XMLHttpRequest, the default character encoding is UTF-8, if the front-end page is gb2312 or other encoding, the obtained data is garbled. Through XMLHttpRequest, post data is also UTF-8 encoding, if the background is gb2312 or other encoding will also appear garbled.

 CacheProblem

Due to the cache Processing Mechanism of IE, the first accessed content is always returned every time a dynamic page is accessed through XMLHttpRequest. The solution is as follows:

1. The client is resolved by adding random strings. For example:
VaR url = 'HTTP: // www.bothv.com /';
URL + = '? Temp = '+ new date (). gettime ();
URL + = '? Temp = '+ math. Random ();
2. Disable caching in HTTP headers. For example:
HTTP:
<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"/>
PHP:
Header ("expires: Thu, 01 Jan 1970 00:00:01 GMT ");
Header ("cache-control: No-cache, must-revalidate ");
Header ("Pragma: No-Cache ");
ASP:
Response. expires = 0
Response. addheader ("Pragma", "No-Cache ")
Response. addheader ("cache-control", "No-cache, must-revalidate ")
JSP:
Response. addheader ("cache-control", "No-Cache ");
Response. addheader ("expires", "Thu, 01 Jan 1970 00:00:01 GMT ");
3. Add:
XMLHttpRequest. setRequestHeader ("If-modified-since", "0 ");
XMLHttpRequest. Send (null );
Ajax cache and encoding are not difficult to solve. The following is a solution.

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.