The code is very concise, here is not much nonsense, directly on the source
HTML code
The
code is as follows:
<!doctype html>
<html>
<head>
<meta charset= "Utf-8"/>
</head>
<body>
<button type= "button" onclick= "Show ()" > Request data </button>
<script src= "Ajax.js" ></script>
<script>
function Show () {
Ajax (' read.txt?datetime=new date.gettime ', function (str) {alert (str);},function () {alert (' failed ');})
};
</script>
</body>
</html>
JavaScript code
The
code is as follows:
function Ajax (url,fnsucc,fnfaild)
{
//1. Create Ajax Objects
if (window. XMLHttpRequest)
{//code for ie7+, Firefox, Chrome, Opera, Safari
var oajax=new xmlhttprequest ();
}
Else
{//code for IE6, IE5
var oajax=new activexobject ("Microsoft.XMLHTTP");
}
2. Linked server (open server connection)
//open (method, filename, asynchronous transmission)
oajax.open (' Get ', url,true);
//3. Send
Oajax.send ();
//4. Receive return
oajax.onreadystatechange=function ()
{
if (oajax.readystate==4)
{
if (oajax.status==200)
{
FNSUCC (Oajax.responsetext);
}
Else
{
Fnfaild (oajax.status);
}
};
};
}