ajax

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax实例,js</title>
</head>
<body>
<input type="button" id='a' value='ajax'>
</body>
<script>

function myAjax(url,fnSucc,fnFaild){
if(window.XMLHttpRequest){
var oAjax=new XMLHttpRequest();
}
else{
var Ojax=new ActiveXObject('Microsoft.XMLHTTP');
}
// 链接服务器
oAjax.open('GET',url+'?t='+new Date().getTime(),true);
// 发送请求
oAjax.send();
// 接收返回
oAjax.onreadystatechange=function(){
if(oAjax.readyState==4){ //读取完成
if(oAjax.status==200){
// alert('chenggong!'+oAjax.responseText);
fnSucc(oAjax.responseText);
}
else{
fnFaild(oAjax.status);
// alert(oAjax.status);
}
}
};
}

var a=document.querySelector("#a");
a.onclick=function(){
myAjax('text.txt',function(str){
alert(str);
},function(str){
alert(str);
});
}
</script>
</html>

源自智能社

原文地址:https://www.cnblogs.com/jldiary/p/5424367.html