XMLHttpRequest processing XML-formatted return data (sample code) _jquery

Source: Internet
Author: User

Sample code:

Copy Code code as follows:

callback function
function callback () {
alert (xmlhttp.readystate);
5. Receive response data
Determining the state of an object is an interactive completion
if (xmlhttp.readystate = = 4) {
To determine if HTTP interaction is successful
if (Xmlhttp.status = = 200) {
To receive the DOM object of an XML data object using the Responsexml method
var domobj = Xmlhttp.responsexml;
if (domobj) {
<message>123123123</message>
The DOM uses getElementsByTagName to get the element node based on the tag name, and returns an array
var messagenodes = domobj.getelementsbytagname ("message");
if (Messagenodes.length > 0) {
Get 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, and FirstChild can get to the first child node of the current node
You can get to the node that corresponds to the text content in the following ways
var textnode = Messagenodes[0].firstchild;
For text nodes, you can return the text content of a text node in a nodevalue way
var responsemessage = Textnode.nodevalue;

Display data on a page
Find the element node for the DIV tag through the DOM
var divnode = document.getElementById ("result");
To set the HTML content in an 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!!! ");
}
}
}

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.