Ajax XMLHTTPRequest object attributes and simple examples

Source: Internet
Author: User

From http://www.ajaxsamples.cn/html/6405.html

 

Attributes of the XMLHTTPRequest object:

Onreadystatechange the event handler of the event triggered by each state change.

The string format of responsetext returned data from the server process.

Responsexml is a dom-compatible document data object returned by the server process.

Status Code returned from the server, such as common 404 (not found) and 200 (ready)

String information of the Status text accompanied by the status code

Readystate object status value

0 (not initialized) the object has been created, but has not been initialized (the open method has not been called)

1 (initialization) the object has been created and the send method has not been called

2 (send data) The send method has been called, but the current status and HTTP header are unknown.

3 (in data transmission) Some data has been received. Because the response and HTTP headers are incomplete, an error occurs when retrieving part of data through responsebody and responsetext,

4. After receiving the data, you can use responsexml and responsetext to obtain the complete response data.


 

Simple Example:

Function createxmlhttp () <br/>{< br/> // create an XMLHTTPRequest object in a non-IE browser <br/> If (window. XMLHttpRequest) <br/>{< br/> XMLHTTP = new XMLHttpRequest (); <br/>}< br/> // create an XMLHTTPRequest object in IE browser <br/> If (window. activexobject) <br/>{< br/> try <br/>{< br/> XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); <br/>}< br/> catch (E) <br/>{< br/> try <br/>{< br/> XMLHTTP = new activexobject ("msxml2.xmlhttp"); <br/>}< B R/> catch (Ex) {}< br/>}< br/> function ajaxrequst () <br/>{< br/> createxmlhttp (); <br/> If (! XMLHTTP) <br/>{< br/> alert ("An error occurred while creating the XMLHTTP object! "); <Br/> return false; <br/>}< br/> XMLHTTP. open ("Post", "ABC. XML ", true); <br/> XMLHTTP. onreadystatechange = function () <br/>{< br/> If (XMLHTTP. readystate = 4) <br/>{< br/> If (XMLHTTP. status = 200) <br/>{< br/> document. write (XMLHTTP. responsexml); <br/>}< br/> XMLHTTP. send (null); <br/>}

 

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.