AJax Learning Note Two (function of the onreadystatechange) _ajax related

Source: Internet
Author: User
Tags response code
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
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.