Ajax reads XML documents

Source: Internet
Author: User

<?xml version= "1.0" encoding= "gb2312"?>
<name> Welcome to Www.111cn.net. The development of this site can not be separated from your support. Thank you! </name>

Ajax files

<title> simple ajax effect </title>
<body>
We prepare an empty div. Fill in the returned data!
<div id= "str" style= "border:1px solid;" ></div>
<script language= "javascript tutorial"
var _xmlhttp//xmlhttprequest The global variable for image
// Create XMLHttpRequest in IE, apply 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;
}
}//loops to create a XMLHTTP based on IE browser. End
///If non-IE browsers, create XMLHttpRequest
if (!_xmlhttp && typeof) based on browsers such as Firefox XMLHttpRequest!= "undefined")
{
_xmlhttp=new xmlhttprequest ();
}
/* The above code succeeded in creating a xmlhttprequest image. Applies to all modern browsers
below we begin to specify the server-side page to request and the way it is requested */

/* Start to specify server-side Web pages and send requests */
/* Define the server-side pages to be requested, in order to be straightforward, here I use an XML file. Because he does not
include any of the server-side program languages. Of course, you can use the ASP tutorials,. net,php tutorials and other documents. You need to be prompted: the most positive of the
System Ajax technology, are using XML as a data transmission format. You can use various server-side programs to generate XML format
*/
var url = "Back.xml"
_xmlhttp.open ("Get", url,true);//tested XML file can only be requested in get mode. But using the XML of the program output can use post
//The following is very important suggestion Read onReadyStateChange attribute detailed explanation
_xmlhttp.onreadystatechange = function () {
If (_xmlhttp.readystate==4) {
if (_xmlhttp.status==200) {
var xmldata=_xmlhttp.responsexml;
var my_name = Xmldata.getelementsbytagname (      "Name") [0].firstchild.nodevalue;
document.getElementById ("str"). innerHTML = My_name;
}
}
}
_xmlhttp.send (null);
</script>
All code for this instance is complete. After running the instance, you will see that the div above will have content displayed.
</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.