When a request is sent, the client cannot determine when it will complete the request, so the event mechanism needs to be used to capture the requested State XMLHttpRequest object that provides the onReadyStateChange event implementation. This is similar to the practice of callback functions. The onReadyStateChange event can specify an event handler function to handle the execution result of the XMLHttpRequest object, such as:
Copy Code code as follows:
Ajaxobj=createajaxobject ();
var url= "/mytodoes/fetchtext?id=" +ID;
Ajaxobj.open ("Get", url,true);
Ajaxobj.onreadystatechange=changetabcallback;
Ajaxobj.send (NULL);
Read the details of the consortium, and then look at this example, a better understanding.
The onReadyStateChange event is triggered when the ReadyState property changes, and the readystate value represents the state of the current request, which can be handled differently in an event handler. ReadyState has five desirable values 0: not initialized, 1: Loading, 2: Loading complete, 3: processing; 4: Processing completed. Once the value of the ReadyState property becomes 4, it can be accessed from the response data returned by the server.
Typically, the value of readystate in an event is processed at the end of the request, such as:
Copy Code code as follows:
function Changetabcallback () {
if (ajaxobj.readystate==4) {
Next validation
}
}
Status stores the HTTP request response code returned by the server side, which represents the processing result of the request, and the common response code has the same meaning as the right.
In AJAX development, the most commonly used is the 200 response code, the following code
Copy Code code as follows:
function Changetabcallback () {
if (ajaxobj.readystate==4) {
if (ajaxobj.status==200) {
The server returned the correct data and began responding to the processing
}
}
}
HTTP status code meaning
200 Request succeeded
202 request accepted but processing not completed
400 Error request
404 Request Resource Not found
500 Internal Server Error