For beginners of Ajax, we need to understand and clarify how to Create Ajax step by step and how to apply Ajax. Next we will start to learn its practical steps.
1. First, we need to understand that AJAX is an Asynchronous JavaScript call. Therefore, we need to understand that AJAX is best suited for asynchronous calls and partial updates in application scenarios.
Ajax is generally used to create an XMLHTTPRequest object, that is, to create an XMLHTTPRequest object.
2. Create an XMLHTTPRequest object
In ie, the XMLHTTPRequest object is an object created by an ActiveX Activity component. In Firefox, the XMLHttpRequest component can be called directly, but IE7 has this component.
However, note that different XMLHttpRequest objects exist in browsers: In the following example, an XMLHTTPRequest object is created using IE. This object is created using different ie versions, therefore, we write it as a method for creating XMLHttpRequest.
VaR XMLHTTP = NULL;
Function createxmlhttp ()
{
If (window. activexobject)
{
VaR arractivex = ['Microsoft. XMLHTTP ', 'msxml2. xmlhttp.6.0 ', 'msxml2. xmlhttp.5.0 ', 'msxml2. xmlhttp.4.0 ', 'msxml2. xmlhttp.3.0 ', 'msxml2. XMLHTTP '];
For (VAR I = 0; I <arractivex. length; I ++)
{
Try
{
XMLHTTP = new activexobject (arractivex [I]);
Break;
}
Catch (Ex)
{
}
}
}
}
2. After the object is created successfully, determine the asynchronous call status, that is, the function that responds to the XMLHttpRequest status change.
The value of the readystate attribute of the XMLHTTPRequest object is used to determine whether the asynchronous call is complete.
Value of the readystate attribute: 0 indicates that the XMLHTTPRequest object is initialized.
1 indicates that initialization is complete.
2. indicates that the object is being sent.
3. indicates that the instance is in the accept status.
4. asynchronous call completion
3. When the asynchronous call is completed, it does not mean that the execution is successful. Therefore, you must determine whether the execution is successful.
Use the status value of the XMLHTTPRequest object to determine the status.
200 indicates success
0 indicates no request status.
Generally, it is used to determine whether an asynchronous call is successful.
4. Data is accepted after the asynchronous call is successful.
Use the responsetext text attribute of the XMLHTTPRequest object or the reponsexml attribute to accept the value "only these two methods"
The above describes how to Create Ajax and applications.
Next we will start to implement the call
First, call the create function to generate the XMLHTTPRequest object.
Ii. Determine whether the XMLHTTPRequest object is successfully created
3. Get the function for changing the object status and onreadystatechange = method of the XMLHTTPRequest object
4. Create an HTTP request XMLHttpRequest open () method
5. Send the send () method of the XMLHttpRequest HTTP Request
6. Use DOM technology for local update