ajax|web| detailed
The hot topic of the Internet this year is of course about the application of WEB2.0, where Ajax is one of the core of WEB2.0. Ajax is the abbreviation for asynchronous JavaScript and XML.
It's not a new language or technology, it is actually a number of techniques that are combined in a certain way to play their part in a collaborative collaboration that includes standardizing rendering using XHTML and CSS, using DOM for dynamic display and interaction, and data exchange and processing using XML and XSLT ; Use XMLHttpRequest for asynchronous data reading; Finally, all data is bound and processed with JavaScript.
Ajax works by adding a middle tier between the user and the server, enabling the user to respond asynchronously to the server. In this way, some of the previous server burden of work to the client, the client idle processing capacity to deal with, reduce the burden of server and bandwidth, so as to save the ISP space and bandwidth rental costs.
We use two examples to verify the existence of the pass account to illustrate the application of Ajax in practice:
(1) A text string to return the response of the server to verify the existence of NetEase pass account;
(2) To return the response to XmlDocument object to verify the existence of Jinshan pass account;
First, we need JavaScript to create the XMLHttpRequest class to send an HTTP request to the server, the XMLHttpRequest class is first introduced by Internet Explorer as an ActiveX object, known as XMLHTTP. Later Mozilla﹑netscape﹑safari and other browsers also provided XMLHttpRequest classes, but they created different methods of XMLHttpRequest classes.
For Internet Explorer browsers, create the XMLHttpRequest method as follows:
Xmlhttp_request = new ActiveXObject ("msxml2.xmlhttp.3.0"); 3.0 or 4.0, 5.0
Xmlhttp_request = new ActiveXObject ("Msxml2.xmlhttp");
Xmlhttp_request = new ActiveXObject ("Microsoft.XMLHTTP");
Because the XMLHTTP version may not be consistent in different Internet Explorer browsers, for better compatibility with different versions of Internet Explorer browsers, we need to base different versions of the Internet Explorer browser to create the XMLHttpRequest class, the code above is a way to create XMLHttpRequest classes based on different Internet Explorer browsers.
For browsers such as Mozilla﹑netscape﹑safari, create the XMLHttpRequest method as follows:
Xmlhttp_request = new XMLHttpRequest ();
If the server's response does not have an XML Mime-type header, some Mozilla browsers may not work correctly. To solve this problem, if the header of the server response is not text/xml, other methods can be invoked to modify the header.
Xmlhttp_request = new XMLHttpRequest ();
Xmlhttp_request.overridemimetype (' Text/xml ');
In practical applications, in order to be compatible with many different versions of browsers, the method of creating the XMLHttpRequest class is generally written as follows:
try{
if (window. ActiveXObject) {
for (var i = 5; i; i--) {
try{
if (i = = 2) {
Xmlhttp_request = new ActiveXObject ("Microsoft.XMLHTTP");
}else{
Xmlhttp_request = new ActiveXObject ("msxml2.xmlhttp." + i + ". 0");
}
Xmlhttp_request.setrequestheader ("Content-type", "Text/xml");
Xmlhttp_request.setrequestheader ("Content-type", "gb2312");
Break
}
catch (e) {
Xmlhttp_request = false;
}
}
}else if (window. XMLHttpRequest) {
Xmlhttp_request = new XMLHttpRequest ();
if (Xmlhttp_request.overridemimetype) {
Xmlhttp_request.overridemimetype (' Text/xml ');
}
}
}catch (e) {xmlhttp_request = false;}
After you have defined how to handle the response, you send the request. You can call the open () and send () methods of the HTTP request class, as follows:
Xmlhttp_request.open (' Get ', URL, true); Xmlhttp_request.send (NULL);
The first parameter of open () is the way the HTTP request-get,post or any server supports that you want to invoke. This argument is capitalized according to the HTTP specification, otherwise some browsers, such as Firefox, may not be able to process the request.
The second parameter is the URL of the request page.
The third parameter sets whether the request is asynchronous mode. If the True,javascript function continues to execute without waiting for the server to respond. This is "A" in "AJAX".
[1] [2] [3] Next page