ajax中responseText返回整个asp~~急待解决~~

代码如下:-----XmlHttpRequest.js-----varxHRObject=false;if(window.XMLHttpRequest){xHRObjec... 代码如下:
-----XmlHttpRequest.js-----
var xHRObject = false;

if (window.XMLHttpRequest)
{
xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}

function sendRequest(data)
{

xHRObject.open("GET", "display.aspx?value=" + data, true);
xHRObject.onreadystatechange = getData;
xHRObject.send(null);
}

function getData()
{
if (xHRObject.readyState == 4 && xHRObject.status == 200)
{
var serverText = xHRObject.responseText;
alert("["+xHRObject.responseText +"]")

if(serverText.indexOf("|") != -1) //检查输出中是否有"|", 没有则返回-1
// indexof某个指定的字符串值在字符串中首次出现的位置
{
element = serverText.split("|");//从:"|"处开始分割,把字符串分割成字符串数组
document.getElementById(element[0]).innerHTML = element[1];
}
}
}
----------------dispaly.aspx-----------
<%@Page Language = "C#" Debug="true" %>

<script language="C#" runat="server">
void Page_Load()
{
switch (Request.Params["value"])//获取url中value的值,并判断
{
case "Contacts":
Response.Write("box1|<br><b>Contacts</b><br>William Moe 1, Acacia Avenue<br>Jane Doe, 2 Willow Tree Lane");
Response.Close();
break;
case "Calendar":
string dt = DateTime.Now.ToString();
Response.Write("box2|<br><b>Calendar:</b><br>" + dt);
Response.Close();
break;
case "Adverts":
string source = "wrox_logo.gif";
Response.Write("box3|<br><b>Advert:</b><br><img src='" + source + "'>");
Response.Close();
break;
}
}
</script>
-----------------------display.htm------------
<html>
<head>
<script type="text/javascript" src="XmlHttpRequest.js"></script>
</head>
<body>

<a href="#" onclick ="sendRequest('Contacts');return false;">Contacts</a>
<a href="#" onclick ="sendRequest('Calendar');return false;">Calendar</a>
<a href="#" onclick ="sendRequest('Adverts');return false;">Adverts</a>
<br/>
<div id="box1">
</div>
<div id="box2">
</div>
<div id="box3">
</div>

</body>
</html>

我用alert检测responsetext的返回值,显示的是display.aspx文件的 所有字符 而不是box1|<br><b>Contacts</b><br>William Moe 1, Acacia Avenue<br>Jane Doe, 2 Willow Tree Lane
有人说要加end,end不是java里面的方法,我加了close 但是都不行 依然返回整个asp文件。
求助大侠们啊~~我困扰了好久了~
展开
 我来答
百度网友6c3218b104
2010-02-01 · TA获得超过138个赞
知道答主
回答量:134
采纳率:0%
帮助的人:108万
展开全部
把这段改成
<script language="C#" runat="server">
void Page_Load()
{
switch (Request.Params["value"])//获取url中value的值,并判断
{
case "Contacts":
Response.Write("box1|<br><b>Contacts</b><br>William Moe 1, Acacia Avenue<br>Jane Doe, 2 Willow Tree Lane");
Response.Close();
break;
case "Calendar":
string dt = DateTime.Now.ToString();
Response.Write("box2|<br><b>Calendar:</b><br>" + dt);
Response.Close();
break;
case "Adverts":
string source = "wrox_logo.gif";
Response.Write("box3|<br><b>Advert:</b><br><img src='" + source + "'>");
Response.Close();
break;
}
}
</script>
改成
<%
void Page_Load()
{
switch (Request.Params["value"])//获取url中value的值,并判断
{
case "Contacts":
Response.Write("box1|<br><b>Contacts</b><br>William Moe 1, Acacia Avenue<br>Jane Doe, 2 Willow Tree Lane");
Response.Close();
break;
case "Calendar":
string dt = DateTime.Now.ToString();
Response.Write("box2|<br><b>Calendar:</b><br>" + dt);
Response.Close();
break;
case "Adverts":
string source = "wrox_logo.gif";
Response.Write("box3|<br><b>Advert:</b><br><img src='" + source + "'>");
Response.Close();
break;
}
}
%>
这样再试试看 如果没有对的话 你可以直接访问display.aspx输入参数看看页面输出的源代码 (源代码就是xhr.responseText的值)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式