) Ajaxxmlhttp: Understand the rare HTTP requests used in Ajax.

Source: Internet
Author: User

Many Web developers only need to generate simple requests and receive simple responses. However, Ajax developers must fully understand the HTTP status.Code, Ready state, and XMLHttpRequest object. In this article, Brett McLaughlin will introduce you to various status codes and show you how the browser processes them. This article also shows the rare HTTP requests used in Ajax.

In the first part of this seriesArticleThe XMLHTTPRequest object, which is an Ajax applicationProgramIs responsible for processing requests from server applications and scripts, and processing data returned from server components. Because all Ajax applications use the XMLHTTPRequest object, you may want to familiarize yourself with this object so that AJAX can perform better.

In this article, I will focus on the three key parts of this request object based on the previous article:

· HTTP readiness
· HTTP status code
· Types of requests that can be generated

These three parts are all factors to consider when constructing a request, but there is little content to introduce these topics. However, if you want to know more about Ajax programming, you need to be familiar with the readiness status, status code, and request content. When an application encounters a problem -- this problem always exists -- if you can correctly understand the meaning of the ready state, how to generate a head request, or 400 status code, you can debug the problem within five minutes, instead of spending five hours in various setbacks and confusions.

Next, let's first look at the HTTP readiness status.

Learn more about HTTP readiness

You should remember that in the previous article, the XMLHTTPRequest object has an attribute named readystate. This attribute ensures that the server has completed a request. Generally, a callback function is used to read data from the server to update web forms or page content. Listing 1 provides a simple example (this is also an example in the previous article in this series-see references ).

XMLHttpRequest or XMLHTTP: Rose

Microsoft and Internet Explorer use an object named XMLHTTP instead of the XMLHTTPRequest object, whereas Mozilla, opera, Safari, and most non-Microsoft browsers use the latter. For simplicity, I simply call both objects XMLHttpRequest. This is consistent with what we see on the web and Microsoft's intention to use XMLHttpRequest as the request object in Internet Explorer 7.0. (For more information about this issue, see section 2nd .)

List 1. process the server response in the callback function

Function updatepage (){
If (request. readystate = 4 ){
If (request. Status = 200 ){
VaR response = request. responsetext. Split (\ "| \");
Document. getelementbyid (\ "Order \"). value = response [0];
Document. getelementbyid (\ "address \"). innerhtml =
Response [1]. Replace (// \ n/g, \ "<br/> \");
} Else
Alert (\ "status is \" + request. status );
}
}

[Page]

This is obviously the most common (and simplest) Use of readiness. As you can see from numbers \ "4 \", there are several other readiness states (you have seen this list in the previous article-see references ):

· 0: the request has not been initialized (open () has not been called ()).
· 1: The request has been created but has not been sent (send () has not been called ()).
· 2: The request has been sent and is being processed (the content header can be obtained from the response ).
· 3: The request is being processed. Generally, some data in the response is available, but the server has not yet completed the response generation.
· 4: The response has been completed. You can obtain and use the server response.

If you want to understand more than just the basic knowledge of Ajax programming, you need to know not only these statuses, but also when they occur and how to use them. First, you need to learn which request status you may encounter in each ready state. Unfortunately, this is not intuitive and involves several special cases.

Secret ready status

The first readiness state is characterized by the readystate attribute 0 (readystate = 0), indicating that the State is not initialized. Once open () is called for the request object, this attribute is set to 1. Since you usually call open () immediately after a pair of requests are initialized, The readystate = 0 is rarely seen. In addition, the uninitialized readiness status is not really useful in the actual application.

However, to satisfy our interests, see the content in Listing 2, which shows how to obtain this readiness state when readystate is set to 0.

Listing 2. Getting the 0-ready status

Function getsalesdata (){
// Create a request object
Createrequest ();
Alert (\ "ready state is: \" + request. readystate );

// Setup (initialize) the request
VaR url = \ "/boards/servlet/updateboardsales \";
Request. Open (\ "Get \", URL, true );
Request. onreadystatechange = updatepage;
Request. Send (null );
}

In this simple example, getsalesdata () is a function used by a Web page call to start a request (for example, when a button is clicked. Note that you must check the readiness status before calling open. Figure 1 shows the result of running the application.

Figure 1. readiness status 0

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.