Ajax reads server-side XML data and parses XML

Source: Internet
Author: User



<title>ajax read data to table </title>

<body>
<input type= "button" value= "Display Data" onclick= "post ()"/>
<script type= "text/web Effects" >
function Ajax_ XMLHTTP () {
//create XMLHttpRequest in IE for all versions above ie5.0
var msxmlhttp = new Array ("msxml2.xmlhttp.5.0", " msxml2.xmlhttp.4.0 "," msxml2.xmlhttp.3.0 "," Msxml2.xmlhttp "," Microsoft.XMLHTTP ");
for (var i=0; i<msxmlhttp.length; i++) {
Try
{
_xmlhttp=new ActiveXObject (msxmlhttp[i));
}
catch (e)
{
_xmlhttp=null;
}
}//Loop create XMLHTTP based on IE browser. End
///If not IE browser, create XMLHttpRequest
if (!_xmlhttp && typeof XMLHttpRequest!= "undefined")
{
_xmlhttp=new xmlhttprequest ();
}
Return _xmlhttp;
}

//Send Request functions
function post () {
var ajax = Ajax_xmlhttp ();//Assign a XMLHttpRequest object to a variable.
Ajax.open ("Post", "Web_ajax.asp Tutorial", true);/Set request mode, request file, asynchronous request
Ajax.onreadystatechange = function () {// You can also specify an already-written function name
if (ajax.readystate==4) {//Data return Success
if (ajax.status==200) {//http Request status code returns OK
var xmldata = ajax.responsexml;//receives the return XML format data
var read = Xmldata.getelementsbytagname ("read");//gets all the read tags
if (read.length !=0) {
var t = document.createelement ("table");//create a TABLE element
T.setattribute ("Border", "1");
Document.body.appendchild (t);//Add table to Doby
for (var i=0;i<read[0].childnodes.length;i++) {
var tr = T.insertrow (t.rows.length);//Add a row
var td = Tr.insertcell (0);//Add a column
td.innerhtml = read[0].childnodes[i]. firstchild.nodevalue;//Write text content to cell
}
}
}
}
}
Ajax.send (null);
}
</script>
</body>

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.