Ajax XMLHTTP Read XML data

Source: Internet
Author: User
Tags http request xml example

<title> Create an available Xmlhttpreuqest object </title>
< body>
<div id= "str" ></div><!--requested data will be displayed in the div-->
<input type= "button" value= "Show Data" Onclick= "post ()"/>
<script type= "text/web Effects" >
function Ajax_xmlhttp () {
//create XMLHttpRequest in IE Applies to all versions of ie5.0 above
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 function
Function post () {
var ajax = Ajax_xmlhttp (); Assigns 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 name
if (ajax.readystate==4) {//data returned successfully
if (ajax.status==200) {//http Request status code returns OK
var xmldata = Ajax.responsexml;
var msg = xmldata.getelementsbytagname ("msg");//Get all MSG elements
var data = Msg[0].firstchild.nodevalue;
document.getElementById ("str"). innerHTML = data;
}
}
}
Ajax.send (NULL);
}
</script>
</body>


XML document Data

<?xml version= "1.0" encoding= "gb2312"?>
<body>
<msg> A simple ASP output XML example, Later in our Ajax tutorial example, we will use this file for data read operations </msg>
<read>
<li>html</li>
<li>css Tutorials </li>
<li>dom</li>
<li>javascript</li>
<li>ajax</li>
</read>
</body>

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.