我6年前
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax</title>
</head>
<body>
<script>
window.onload = function(){
var btn = document.getElementById("btn");
btn.onclick = function(){
// var xhr = null;
// if(window.XMLHttpRequest){
// var xhr = new XMLHttpRequest();
// }else{
// var xhr = new ActiveXObject('Microsoft.XMLHTTP');
// }
try{
var xhr = new XMLHttpRequest();
}catch(e){
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
}
xhr.open('get','ajax.txt',true);
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState == 0){
alert("请求未初始化");
}else if(xhr.readyState == 1){
alert("服务器连接已建立");
}else if(xhr.readyState == 2){
alert("请求已接收");
}else if(xhr.readyState == 3){
alert("请求处理中");
}else{
alert(xhr.responseText);
}
}
}
}
</script>
<input type="button" value="获取ajax" id="btn">
</body>
</html>
0