A simple example of reading an XML file with Ajax _ajax related

Source: Internet
Author: User
This allows you to send requests to read server-side XML data, and the last thing to do is to process the data. For XMLHttpRequest objects, refer to the About XMLHttpRequest object.

See Example:

Ajaxdemo.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>asynchronous JavaScript and Xml</title>
<body>
<script type= "Text/javascript" >
var xmlhttp=null;
function Readystatechangehandle ()
{
if (xmlhttp.readystate==4)
{
if (xmlhttp.status==200)
{
var xmldom=xmlhttp.responsexml;
var xmlroot=xmldom.documentelement;
Try
{
var xmlitem=xmlroot.getelementsbytagname ("item");
alert (xmlitem[0].firstchild.data);
}
catch (E)
{
alert (e.message);
}
}
}
}
function Ajaxrequest ()
{
if (window. XMLHttpRequest)
{
Xmlhttp=new XMLHttpRequest ();
}
else if (window. ActiveXObject)
{
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}
Xmlhttp.onreadystatechange=readystatechangehandle;
Xmlhttp.open ("Get", "Data.xml", true);
Xmlhttp.send (NULL);
}
</script>
<input type= "button" onclick= "Ajaxrequest ()" value= "Take me to the world of AJAX"/>
</body>
Data.xml
<?xml version= "1.0" encoding= "GB2312"?>
<root>
<item>welcome to the world of AJAX (asynchronous JavaScript and XML)!</item>
</root>
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.