自己在实验一个ajax的小程序时,为什么点击出现的是info.asp的代码呢?
index.html如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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如下:
<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%>
<!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>
</head>
<body>
<%
Response.Write("zhaolianxiang,你好吗")
%>
</body>
</html>
为什么我测试的结果出现的是代码呢???
------解决方案--------------------
document.getElementById("div1").innerText = response;
你用的是innerText
改成innerHTML