网络编程 发布日期:2025/11/5 浏览次数:1
复制代码 代码如下: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>无标题文档</title> 
<script> 
window.onload = function(){ 
var oBtn = document.getElementById('btn'); 
oBtn.onclick = function(){ 
//1.创建ajax对象 
//只支持非IE6浏览器 
var oAjax = null; 
if(window.XMLHttpRequest){ 
oAjax = new XMLHttpRequest(); 
//alert(new XMLHttpRequest()); 
}else{ 
//只支持IE6浏览器 
oAjax = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
//2.连接服务器,这里加个时间参数,每次访问地址都不一样,浏览器就不用浏览器里的缓冲了,但 
// 但服务器那端是不解析这个时间的 
oAjax.open("get","a.txt?t=" + new Date().getTime(),true); 
//3.发送 
oAjax.send(null); 
//4.接受信息 
oAjax.onreadystatechange = function(){ 
//浏览器与服务器之间的交互,进行到哪一步了,当等于4的时候,代表读取完成了 
if(oAjax.readyState==4){ 
//状态码,只有等于200,代表接受完成,并且成功了 
if(oAjax.status==200){ 
alert("成功" + oAjax.responseText); 
}else{ 
alert("失败"); 
} 
} 
}; 
}; 
}; 
</script> 
</head> 
<body> 
<input type="button" value="按钮" id="btn"/> 
</body> 
</html>