Functions of onreadystatechange in Ajax

Source: Internet
Author: User
Tags response code

After a request is sent, the client cannot determine when the request will be completed. Therefore, an event mechanism is required to capture the Request status. The XMLHTTPRequest object provides the onreadystatechange event to implement this function. This is similar to the callback function.
The onreadystatechange event specifies an event processing function to process the execution results of the XMLHTTPRequest object, for example:
Ajaxobj = createajaxobject ();
VaR url = "/mytodoes/fetchtext? Id = "+ ID;
Ajaxobj. Open ("get", URL, true );
Ajaxobj. onreadystatechange = changetabcallback;
Ajaxobj. Send (null );

Onreadystatechange

The event is triggered when the readystate attribute changes. The value of readystate indicates the status of the current request.ProgramYou can perform different processing based on this value.
Readystate can be set to 0: not initialized, 1: Loading, 2: Loading, 3: processing, and 4: processing. Once the value of the readystate attribute changes
4. You can access the response data returned by the server.
The value of readystate in an event is usually processed only after the request is completed, for example:
Function changetabcallback (){
If (ajaxobj. readystate = 4 ){
// Perform the next Verification
}
}

Status stores the HTTP request response returned by the server. Code indicates the request processing result, the meaning of common response codes is as follows: Right.
in Ajax development, the most common response code is 200. The Code is as follows:
function changetabcallback () {
If (ajaxobj. readystate = 4) {
If (ajaxobj. status = 200) {
// The server returns the correct data, start Response Processing
}< BR >}< br> HTTP status code meaning
200 successful request
202 request accepted but 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.