一、简单请求实例
<!DOCTYPE html>
<head>
<title>一个简单的异步请求示例</title>
<script type="text/javascript">
var xmlHttpObject;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttpObject = new XMLHttpRequest();
}
}
function startRequest() {
createXMLHttpRequest();
xmlHttpObject.onreadystatechange = handleStateChange;
xmlHttpObject.open("GET", "http://code.ybao.org/demo/src/simpleResponse.txt", true);
xmlHttpObject.send(null);
}
function handleStateChange() {
if(xmlHttpObject.readyState == 4) {
if(xmlHttpObject.status == 200) {
alert("服务器响应为:" + xmlHttpObject.responseText);
}
}
}
</script>
</head>
<body>
<form action="#">
<input type="button" value="发送异步请求" onClick="startRequest();"/>
</form>
</body>
</html>