Simple Ajax development framework

Source: Internet
Author: User
Ajax is a comprehensive technology. In the process of using Ajax, the first step is to add the corresponding event trigger attribute to the HTML control to enable it to trigger events. The second is to compile the event processing function, initialize the XMLHTTPRequest object in this function, specify the response handler, create an HTTP request, set the necessary HTTP request header information, and send the created HTTP request to the server; the last step is to create a response processing function, which makes a set business processing transaction based on the judgment of the HTTP Request status.
(1) event processing functions:
A) initialize the XMLHTTPRequest object;
The implementation code is as follows:
If (widow. XMLHttpRequest) {// Mozilla, Safari ,.......
Http_request = new XMLHttpRequest ();
} Else if (window. activexobject) {// IE
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Else {
Window. Alert ("the browser does not implement the XMLHttpRequest component object! ");
}
B) set the onreadystatechange attribute of the XMLHTTPRequest object and specify the callback function to be called when the server returns the response data, that is, the response processing function;
The implementation code is as follows:
Http_request.onreadystatechange = processrequest;
C) Call the open method of the XMLHTTPRequest object to create an HTTP request;
The implementation code is as follows:
Http_request.open ("get", "http://www.example.org/some.file", true );
D) Call the setRequestHeader and other methods of the XMLHTTPRequest object to set the necessary HTTP request header information;
The implementation code is as follows:
Http_request.setrequestheader ("Content-Type", application/X-WWW-form-urlencoded );
E) call the send method of the XMLHTTPRequest object to send the previously created HTTP request;
The implementation code is as follows:
Http_request.send (null );
F) determines whether to wait for the server to return response data based on the open method parameters of the XMLHTTPRequest object.
(2) The callback function (Response Processing Function) executes the following transactions:
Aa) determines the status of the HTTP request and processes it accordingly;
Bb) Call the responsexml or responsetext method of the XMLHTTPRequest object, and assign the response data returned by the server to the Javascript variable or object;
CC) uses Dom or other methods to parse the response data returned by the server and save it in the cache variables or arrays;
Dd) parse HTML documents using Dom to locate the target HTML document node;
Ee) use the response data returned by the parsed server to update the attribute values or content of the HTML document node in the previous step.

 

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.