On the realization method and principle mechanism of Ajax

Source: Internet
Author: User

On the realization method and principle mechanism of Ajax

The name Ajax is said to be a shorthand for asynchronous Web page effects + XML, in fact, it is a combination of the following several technologies.

1. Use CSS Tutorials and XHTML for presentation.
2. Use the DOM model for interactive and dynamic display.
3. Use XMLHttpRequest to communicate with the server asynchronously.
4. Use JavaScript to bind and invoke.

The principle of Ajax
XMLHttpRequest is the core mechanism of Ajax, which is introduced first in IE5 and is a technology that supports asynchronous requests. Simply put, JavaScript is the time to request and process responses to the server without blocking the user. No refreshing effect is achieved.
So let's start with XMLHttpRequest and see how it works.
First, let's look at the properties of the XMLHttpRequest object.
Its properties are:
onReadyStateChange the event handler for the event that triggers each state change.
ResponseText A string form that returns data from a server process.
Responsexml a DOM-compliant document data object returned from a server process.
Status number code returned from the server, such as common 404 (not Found) and 200 (ready)
Status text string information accompanying the status code
ReadyState object state Value, 0-Uninitialized 1-Loading 2-loading complete 3-Interactive 4-complete.

However, because of the differences between browsers, creating a XMLHttpRequest object may require different methods. This difference is mainly reflected in IE and other browsers.
The following are XMLHttpRequest objects created separately from different browsers.


The following are XMLHttpRequest objects created separately from different browsers.

<script language= "javascript" type= "Text/javascript" >
var xmlhttp = false;
Create a XMLHttpRequest object for IE
try {
Use a version of MSXML to create
XMLHTTP = new ActiveXObject ("Msxml2.xmlhttp");
catch (e) {
try {
Use one of its other objects to create
XMLHTTP = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (E2) {
XMLHTTP = false;
}
}

if (!xmlhttp && typeof xmlhttprequest!= ' undefined ') {
Create a XMLHttpRequest object for other non-Microsoft browsers
XMLHTTP = new XMLHttpRequest ();
}
</script>

This process is divided into three steps, first we define a XMLHTTP to reference the created XMLHttpRequest. We then try to create the object in a Microsoft browser, first with the Msxml.xmlhttp object, and then try to create it with macrosoft.xmlhttp if it fails. Finally, we create this object for non-Microsoft browsers.
So, we've created a XMLHttpRequest object, so let's look at how to make a XMLHttpRequest request. function

Executexmlhttprequest (Callback,url)
{
Handling non-Microsoft browsers
if (window.xmlhttprequest)
{
Xhr=new XMLHttpRequest ();
Xhr.onreadystatechange = callback;
Xhr.open ("Get", url,true);
Xhr.send (NULL);
}
Dealing with Microsoft browsers
else if (window.activexobject)
{
Xhr=new ActiveXObject ("macrosoft.xmlhttp");
if (XHR)
{
Xhr.onreadystatechage=callback;
Xhr.open ("Get", url,true);
Xhr.send ();
}
}


Visible from above, execution XMLHttpRequest in fact, most of the code is still used to deal with the difference between the browser, for different browsers it still has to do different processing, but this looks very intuitive.
In the above code, the key is:

Xhr.onreadystatechage=callback It defines a callback function that is automatically executed once it is answered.
Xhr.open ("" Get ", url,true); True indicates that you want to execute the request asynchronously.

For callback, we have:
function Processajaxresponse () {
Status identified as Completed
if (xhr.readystate = = 4) {
is ready
if (Xhr.status = = 200) {
502 502 ' votes '). innerHTML = Xhr.responsetext;
} else {
Alert ("There was a problem retrieving the XML data:
" +
Xhr.statustext);
}
}
}


That is, once the server finishes processing XMLHttpRequest and returns to the browser, the callback method assigned with Xhr.onreadystatechange will be invoked automatically.

This is almost the entire workflow of XMLHttpRequest, which first checks the overall state of the XMLHttpRequest and ensures that it is completed (readystatus=4), and then asks for the status of the request based on the server's settings, if everything is ready (status =200), then perform the actions that are required below.

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.