index.asp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>AJAX简单应用</title>
<script language="javascript">
//创建XMLHttpRequest对象
var request = false;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = false;
}
}
}
if (!request)alert("Error initializing XMLHttpRequest!");
//获取信息
function getInfo() {
var url = "info.asp?id=" + Math.random();
request.open("GET", url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
//更新页面
function updatePage() {
if (request.readyState == 4) {
if (request.status == 200) {
var response = request.responseText;
document.getElementById("div1").innerText = response;
}else if(request.status == 404) {
alert ("Requested URL is not found.");
} else if (request.status == 403) {
alert("Access denied.");
} else
alert("status is " + request.status);
}
}
</script>
</head>
<body>
<input name="btn" type="button" id="btn" value="获取" onclick="getInfo();" />
<div id="div1">此处显示新 Div 标签的内容</div>
</body>
</html>
info.asp
<%
Response.Charset = "GB2312" '不设置中文会乱码
Dim i,arr(9)
arr(0) = "000000000000000000"
arr(1) = "11111111111111111"
arr(2) = "222222222222222222"
arr(3) = "333333333333333333"
arr(4) = "444444444444444444"
arr(5) = "555555555555555555"
arr(6) = "666666666666666666"
arr(7) = "777777777777777777"
arr(8) = "888888888888888888"
arr(9) = "999999999999999999"
Randomize
i = Int(10*Rnd())
Response.write arr(i)
%>