What is the role of onreadystatechange in Ajax?

Source: Internet
Author: User

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. Onreadystatechange event can be used to specify an event handler to process the execution results of the XMLHTTPRequest object, such as ajaxobj = createajaxobject (); var url = "/mytodoes/fetchtext? Id = "+ ID; ajaxobj. open ("get", URL, true); ajaxobj. onreadystatechange = changetabcallback; ajaxobj. send (null); The onreadystatechange event is triggered when the readystate attribute changes. The value of readystate indicates the status of the current request. Program You 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 is changed to 4, you can access the response data returned by the server. The value of readystate is usually processed only when the request is completed, for example, function changetabcallback () {If (ajaxobj. readystate = 4) {// next verification} status stores the HTTP request response returned by the server Code It indicates the request processing result. The meaning of common response code 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} HTTP status code meaning 200 request successful 202 request accepted but not completed 400 Error request 404 request resource not found 500 Internal Server Error

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.