Analysis of Ajax controls and Class Libraries

Source: Internet
Author: User
Http://www.51cto.com Author: Kris hadloc Source: IT expert network

Users' interactions drive the web site. Understanding how to process response information, especially when using new interactive operations (such as Ajax) is very important. Kris hadloc explains the nature of the Ajax request-response process. You should understand this content to better serve user interaction operations.

Requests and responses

The Ajax engine is divided into many aspects, each of which is very important. If the engine executes a transaction that sends a request and receives the response information, it has many methods to process the response information. Response Information is an important part of the processing process, because the user will eventually interact with the response information. This article explains in detail how to handle Ajax response information, provide feedback to users, and update as needed. Starting from the request readiness status, we will then explain the status, callback, and analysis of the response information. This article also explains other aspects of response information, such as loading messages, error processing, and displaying response information.

I have created an example for you to download. This example contains an object-oriented Ajax engine. You can use it again in any Ajax application Program . Before discussing the response information, I would like to point out how to build an Ajax engine and send requests. First, let's take a look at the Code Of the Ajax engine (the Response Information Processing Section is not included):

Document. Write ("");

Function Ajax ()

{

This. tostring = function () {return "ajax ";}

This. Http = new HTTP ();

This. makerequest = function (_ method, _ URL, _ callbackmethod)

{

This. Request = (window. XMLHttpRequest )?

New XMLHttpRequest (): New activexobject ("msxml2.xmlhttp ");

This. Request. onreadystatechange = _ callbackmethod;

This. Request. Open (_ method, _ URL, true );

This. Request. Send (_ URL );

}

}

To create this object and send a request, you only need to use the following two lines of code:

VaR Ajax = new Ajax ();

Ajax. makerequest ('get', 'xml/content. xml', onresponse );

 

The second line of code above reflects the request method you selected, the XML Path or the server-side script of the request, and the callback method you want to call when receiving the response information. Now you have some knowledge about the Ajax engine and how to send requests. Let's take a look at how to process requests.

Ready (ready) Status

The ready status is handled by the callback method. When we make a request, the callback method has been set. In this example, onresponse is set as a callback method, which is used in this article to process all the analysis code operations. We will use the following code to check the Ajax object readiness status in the callback method:

Function onresponse ()

{

If (Ajax. checkreadystate ('body', 'loading... ', 'loading...') = "OK ")

{

// Here is the analysis code

}

}

The code above shows that four parameters will be passed to the checkreadystate method. The first parameter is the ID of the loaded message Div we want to display, and the other three parameters are custom loading messages corresponding to different States. The DIV used to load messages is the body name, which makes the content and loaded messages merged when new data is loaded. The following is the actual checkreadystate method. It processes the code we just discussed and displays it with the parameters passed to us in Div. This method is also included in the sample Ajax engine.

This. checkreadystate = function (_ id, _ 1, _ 2, _ 3)

{

Switch (this. Request. readystate)

{

Case 1:

Document. getelementbyid (_ id). innerhtml = _ 1;

Break;

Case 2:

Document. getelementbyid (_ id). innerhtml = _ 2;

Break;

Case 3:

Document. getelementbyid (_ id). innerhtml = _ 3;

Break;

Case 4:

Document. getelementbyid (_ id). innerhtml = "";

Return this. http. Status (this. Request. status );

}

}

The checkreadystate method is useful for providing Page Status feedback. The following table shows the detected values:

Value Status
0 Uninitialized (not initialized)
1 Loading (loading)
2 Loaded (loaded)
3 Interactive)
4 Complete (complete)

You can add a custom message for each loading status-it can be a simple string or an image tag in string format (such as displaying an animated GIF ). The following is an example:

VaR loader = "" IMG src = "http://images.51cto.com/wuyou/images/loader.gif"> "〉";

Ajax. checkreadystate ('body', loader, loader );

Not only does the checkreadystate method process the Request status, but the HTTP object contained in it also detects and returns the Request status.

Users' interactions drive the web site. Understanding how to process response information, especially when using new interactive operations (such as Ajax) is very important. Kris hadloc explains the nature of the Ajax request-response process. You should understand this content to better serve user interaction operations.

Requests and responses

The Ajax engine is divided into many aspects, each of which is very important. If the engine executes a transaction that sends a request and receives the response information, it has many methods to process the response information. Response Information is an important part of the processing process, because the user will eventually interact with the response information. This article explains in detail how to handle Ajax response information, provide feedback to users, and update as needed. Starting from the request readiness status, we will then explain the status, callback, and analysis of the response information. This article also explains other aspects of response information, such as loading messages, error processing, and displaying response information.

I have created an example for you to download. This example contains an object-oriented Ajax engine that you can use in any Ajax application. Before discussing the response information, I would like to point out how to build an Ajax engine and send requests. First, let's take a look at the Ajax engine code (without the Response Information Processing Section ):

Document. Write ("src = \" JS/HTTP. js \ "> ");

Function Ajax ()

{

This. tostring = function () {return "ajax ";}

This. Http = new HTTP ();

This. makerequest = function (_ method, _ URL, _ callbackmethod)

{

This. Request = (window. XMLHttpRequest )? New XMLHttpRequest ():

New activexobject ("msxml2.xmlhttp ");

This. Request. onreadystatechange = _ callbackmethod;

This. Request. Open (_ method, _ URL, true );

This. Request. Send (_ URL );

}

}

To create this object and send a request, you only need to use the following two lines of code:

VaR Ajax = new Ajax ();

Ajax. makerequest ('get', 'xml/content. xml', onresponse );

The second line of code above reflects the request method you selected, the XML Path or the server-side script of the request, and the callback method you want to call when receiving the response information. Now you have some knowledge about the Ajax engine and how to send requests. Let's take a look at how to process requests.

Ready (ready) Status

The ready status is handled by the callback method. When we make a request, the callback method has been set. In this example, onresponse is set as a callback method, which is used in this article to process all the analysis code operations. We will use the following code to check the Ajax object readiness status in the callback method:

Function onresponse ()

{

If (Ajax. checkreadystate ('body', 'loading... ', 'loading...') = "OK ")

{

// Here is the analysis code

}

}

The code above shows that four parameters will be passed to the checkreadystate method. The first parameter is the ID of the loaded message Div we want to display, and the other three parameters are custom loading messages corresponding to different States. The DIV used to load messages is the body name, which makes the content and loaded messages merged when new data is loaded. The following is the actual checkreadystate method. It processes the code we just discussed and displays it with the parameters passed to us in Div. This method is also included in the sample Ajax engine.

This. checkreadystate = function (_ id, _ 1, _ 2, _ 3)

{

Switch (this. Request. readystate)

{

Case 1:

Document. getelementbyid (_ id). innerhtml = _ 1;

Break;

Case 2:

Document. getelementbyid (_ id). innerhtml = _ 2;

Break;

Case 3:

Document. getelementbyid (_ id). innerhtml = _ 3;

Break;

Case 4:

Document. getelementbyid (_ id). innerhtml = "";

Return this. http. Status (this. Request. status );

}

}

The checkreadystate method is useful for providing Page Status feedback. The following table shows the detected values:

Value Status
0 Uninitialized (not initialized)
1 Loading (loading)
2 Loaded (loaded)
3 Interactive)
4 Complete (complete)

You can add a custom message for each loading status-it can be a simple string or an image tag in string format (such as displaying an animated GIF ). The following is an example:

VaR loader = "" IMG src = "http://images.51cto.com/wuyou/images/loader.gif"> "〉";

Ajax. checkreadystate ('body', loader, loader );

Not only does the checkreadystate method process the Request status, but the HTTP object contained in it also detects and returns the Request status.

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.