Ajax tips for using summary _ajax related

Source: Internet
Author: User
In the use of Ajax process, sometimes always encounter some problems, browser compatibility, coding, ie under the special processing, and so on, and occasionally make people confused, here are some tips, perhaps some use.

Using JavaScript libraries
Ajax's popularity and great power, let us re-examine the development of JavaScript, but also directly promote the emergence of various libraries. For ordinary developers, using some of their own JavaScript libraries can not only avoid problems like browser compatibility on Ajax applications, but also make them more stable and efficient. Here are some of the lightweight JavaScript libraries I know:
1 Yui:yahoo production, the component is rich and strong and stable, is the first choice of team cooperation development.
2 JQuery: Flexible and efficient, its selector syntax engine based on CSS3 and XPath is very powerful and complete.
3 Prototype: Is a very elegant JavaScript library, the most classic is the $ symbol, dwr,jquery are attracted by it. On the basis of it appeared script.aculo.us.
4 Mootools: The core syntax and prototype are similar, but after the use of what is known as simple lightweight and dapper.

Coding problems
Data obtained through XMLHttpRequest, the default character encoding is UTF-8, if the front page is GB2312 or other encoding, the display of the data is garbled. Through the Xmlhttprequest,post data is also UTF-8 code, if the background is GB2312 or other code will also appear garbled. Workaround:
1 reunification to UTF-8. This is also the inevitable trend of internationalization.
2 when outputting text text obtained through XMLHttpRequest, the text declaration is added in headers (direct HTML declaration has no effect). Such as:
Php:header (' content-type:text/html;charset=gb2312 ');
ASP:Response.Charset = "GB2312"
JSP:response.setHeader ("Charset", "GB2312");
3 The mandatory declaration on the WWW server. For example: The configuration under Apache:
Adddefaultcharset GB2312
This is primarily a case where a file accessed via XMLHttpRequest is a static file and cannot be declared headers.
Static page will generally go through Apache deflate or gzip compression, at this point in the case of IE, the first data obtained through XMLHttpRequest can be displayed normally, but then get the data display garbled, this time because the data retrieved from the cache, The default encoding declaration for Apache settings may be lost due to a problem with the browser's decompression. Because this is generally plain text, you may not be able to prevent caching, you can set XMLHttpRequest access to the text file does not compress to solve this problem.

4 Non-UTF-8 pages obtained by XMLHttpRequest text text output before the character transcoding into Unicode, or the encoding is directly UTF-8, can be displayed normally. As shown in the example.

The caching problem under IE
Because of the cache processing mechanism of IE, every time through the XMLHttpRequest access to the dynamic page returned by the Always first access content, the solution is:

1 The client is resolved by adding a random string. such as:
Copy Code code as follows:

var url = ' http://dancewithnet.com/';
url = '? temp= ' + New Date (). GetTime ();
url = '? temp= ' + math.random ();

2 Disables caching in HTTP headers. Such as:
HTTP:
Copy Code code as follows:

<meta http-equiv= "Pragma" content= "No-cache"/>
<meta http-equiv= "Cache-control" content= "No-cache, must-revalidate"/>
<meta http-equiv= "Expires" content= "Thu, 1970 00:00:01 GMT"/>
<meta http-equiv= "Expires" content= "0"/>

Php:
Copy Code code as follows:

Header ("Expires:thu, 1970 00:00:01 GMT");
Header ("Cache-control:no-cache, must-revalidate");
Header ("Pragma:no-cache");

Asp:
Copy Code code as follows:

Response.expires=0
Response.AddHeader ("Pragma", "No-cache")
Response.AddHeader ("Cache-control", "No-cache, Must-revalidate")

Jsp:
Copy Code code as follows:

Response.AddHeader ("Cache-control", "No-cache");
Response.AddHeader ("Expires", "Thu, 1970 00:00:01 GMT");

The problem of reponsexml under IE
When using Responsexml, an XML file with an. xml suffix can only be accepted under IE, if it cannot end with an. xml file, you need to do the following:
1 declaration on the server side is an XML file type. Such as:
Php:header ("Content-type:text/xml;charset=utf-8");
ASP:Response.ContentType = "Text/xml";
JSP:response.setHeader ("ContentType", "Text/xml");
2 is acquired using responsetext and then encapsulated into XML.
3 in AJAX applications, JSON and JSONML are very good alternatives to XML.
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.