The code is very concise, so I won't talk much nonsense here, just click on the source code
html code
The code copy 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 copy is as follows:
function Ajax(url,fnSucc,fnFaild)
{
//1. Create ajax object
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. Link to the server (open the server's connection)
//open(method, file name, asynchronous transmission)
oAjax.open('GET',url,true);
//3.Send
oAjax.send();
//4. Receive and return
oAjax.onreadystatechange=function()
{
if (oAjax.readyState==4)
{
if(oAjax.status==200)
{
fnSucc(oAjax.responseText);
}
else
{
fnFaild(oAjax.status);
}
};
};
}
The requested file is read.txt
Fill in the content casually