XMLHttpRequest processes returned data in xml format (sample code)

Source: Internet
Author: User

Sample Code:
Copy codeThe Code is as follows:
// Callback function
Function callback (){
// Alert (xmlhttp. readyState );
// 5. Receive response data
// Determine whether the object state is interactive.
If (xmlhttp. readyState = 4 ){
// Determine whether http interaction is successful
If (xmlhttp. status = 200 ){
// Use responseXML to receive DOM objects of XML Data Objects
Var domObj = xmlhttp. responseXML;
If (domObj ){
/< Message> 123123123 </message>
// Use getElementsByTagName In the dom to obtain the Element Node Based on the tag name. An array is returned.
Var messageNodes = domObj. getElementsByTagName ("message ");
If (messageNodes. length> 0 ){
// Obtain the text content in the message Node
// The text in the message label is the byte point of the element node corresponding to the message label in the dom. firstChild can obtain the first subnode of the current node.
// The Node corresponding to the text content can be obtained through the following methods
Var textNode = messageNodes [0]. firstChild;
// For a text node, nodeValue can be used to return the text content of the text node.
Var responseMessage = textNode. nodeValue;

// Display data on the page
// Use dom to find the Element Node corresponding to the div tag
Var divNode = document. getElementById ("result ");
// Set the html content in the Element Node
DivNode. innerHTML = responseMessage;
} Else {
Alert ("XML data format error, original text content:" + xmlhttp. responseText );
}
} Else {
Alert ("XML data format error, original text content:" + xmlhttp. responseText );
}
} Else {
Alert ("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.