Full understanding of HTTP status codes, ready status, and XMLHttpRequest objects
Introduction: For many WEB developers, it is only necessary to generate simple requests and receive simple responses, but for developers wishing to master Ajax, the HTTP status code, the ready state, and the XMLHttpRequest object must be fully understood. In this article, Brett McLaughlin introduces you to a variety of status codes and shows how the browser handles them, and this article gives you a rare HTTP request that is used in Ajax.
In the previous article in this series, we will detail the XMLHttpRequest object, which is the center of an AJAX application that handles requests for server-side applications and scripts and processes data returned from server-side components. Because all AJAX applications use the XMLHttpRequest object, you might want to familiarize yourself with the object so that Ajax can perform better.
In this article, I will focus on the contents of the 3 key parts of this request object, based on the previous article:
· HTTP Ready State
· HTTP Status Code
• Types of requests that can be generated
These three sections are the factors to consider when constructing a request, but there is little to introduce them. However, if you want to learn more than just the common sense of Ajax programming, you need to be familiar with the ready state, the status code, and the content of the request itself. When a problem occurs in an application--a problem that always exists--if you can correctly understand the readiness, how to generate a head request, or the exact meaning of the 400 status code, you can debug the problem in 5 minutes instead of spending 5 hours in frustration and confusion.
Let's take a look at the HTTP ready state first.
Deep understanding of HTTP ready State
You should remember that in the previous article the XMLHttpRequest object had a property named ReadyState. This property ensures that the server has completed a request and typically uses a callback function to read the data from the server to update the contents of the Web form or page. Listing 1 gives a simple example (this is an example of the previous article in this series), see Resources.
XMLHttpRequest or XMLHttp: Changing the name of a rose
Microsoft™ and Internet Explorer use an object named XMLHttp instead of a XMLHttpRequest object, while Mozilla, Opera, Safari, and most non-Microsoft browsers use the Post Actors For simplicity's sake, I'll simply refer to both objects as XMLHttpRequest. This is consistent with what we see on the Web and with Microsoft's intent to use XMLHttpRequest as the object of request in Internet Explorer 7.0. (see part 2nd for more information on this issue.) )
Listing 1. Handling the response of a server in a 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);
}
}
This is clearly the most common (and simplest) use of the ready state. As you can see from the number "4", there are several other ready states (which you have seen in the previous article-see Resources):
• 0: The request was not initialized (the open () was not called yet).
• 1: The request has been established, but has not been sent (send () has not been called yet).
• 2: The request has been sent and is being processed (usually the content header can now be fetched from the response).
• 3: The request is in process; some of the data is usually available in the response, but the server has not completed the response generation.
• 4: The response is complete; you can get and use the server's response.
If you want to learn more than just the basics of Ajax programming, you need to know not only these states, when they appear, and how to use them. First, you need to learn what kind of request status you might encounter in each of the ready states. Unfortunately, this is not intuitive and involves several special situations.