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 !!! ");
}
}
}