Parse how Ajax processes Response Information

Source: Internet
Author: User

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("");
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="/wuyou/images/loader.gif"〉";
ajax.checkReadyState('body', loader, 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("");
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="/wuyou/images/loader.gif"〉";
ajax.checkReadyState('body', loader, 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.

Enter the AJAX topic: AJAX technology development (

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.