ASP教程:关于AJAX一个简单的例子
2018-09-06 12:11
ASP教程:关于AJAX一个简单的例子
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;
}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(); />
<p id=p1>此处显示新 Div 标签的内容</p>
</body>
</html>
Info.asp:
程序代码
<%
Response.Charset = GB2312 不设置中文会乱码
Dim i,arr(9)
arr(0) = 随机信息000000000000000000
arr(1) = 随机信息111111111111111111
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)
%>
说明:AJAX对服务器返回的结果没有格式上的要求,只需将结果用Response.write输出即可。