<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>