为什么取xmlHttp.responseXML.documentElement取为空?

liooon 2009-04-09 02:36:50
要求,在asp页面中输入工号项中输入工号,自动在该页面中显示该工号的相关数据,比如对应的姓名,电话等.

想到用Ajax 实现,代码如下:

a.asp页面,该部分HTML代码省略.

.....
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="course_style.css" rel="stylesheet" type="text/css">
<script src="showpersonalinfo.js"></script>
....
<form name="form" method="post" action="sendproc.asp" onSubmit="return chkform();">
...
<td ><input type="text" name="worknum" id="worknum" onKeyUp="showpersonalinfo(this.value)"><td>
<td><input type="text" name="name" id="name"></td>
<td><input name="extension" type="text" id="extension"></td>
....
</form>



showpersonalinfo.js页面

var xmlHttp;

function showpersonalinfo(str)
{
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="getpersonalinfo.asp"
url=url+"?worknum="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=statechanged;
xmlHttp.open("get",url,null);
xmlHttp.send(null);
}

function statechanged()
{
if(xmlHttp.readystate==4)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getelementByid("name").innerHtml=xmlDoc.getelementsBytagname("name")[0].childnodes[0].nodevalue;
document.getelementByid("extension").innerHtml=xmlDoc.getelementsBytagname("extension")[0].childnodes[0].nodevalue;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}


getpersonalinfo.asp页面

<%
response.Expires=-1
response.contenttype="text/xml"
response.Charset="utf-8"
%>

<!--#included file="inc/conn.asp"-->
<!--#included file="inc/function.asp"-->

<%
dim worknum
dim rs,sql

worknum=trim(request.QueryString("worknum"))

set rs=server.CreateObject("adodb.recordset")
sql="select * from employee where wcode='"&worknum&"'"

rs.open sql,conn,1,3

if not(rs.bof and rs.eof) then
response.write("<?xml version='1.0' encoding='utf-8'?>")
response.write("<personalinfo>")
response.write("<name>"&rs("name")&"</name>")
response.write("<division>"&rs("div")&"</division>")
response.write("<dept>"&rs("dept")&"</dept>")
response.write("<extension>"&rs("extension")&"</extension>")
response.write("<email>"&rs("email")&"</email>")
response.write("</personalinfo>")
end if
rs.close
set rs=nothing
%>


问题:如上代码蓝色行,xmlHttp.responseXML.documentElement返回的值这空,用alert输出xmlHttp.responsetext有值返回,alert输出xmlHttp.responseXML为[object],不知道这是什么原因,返回xmlHttp.responseXML.documentElement的值为NULL?
...全文
2899 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zk247444107 2012-01-16
  • 打赏
  • 举报
回复
请仔细验证响应xml内容是否有误,可以把xml保存成一个文件,然后直接用IE打开进行验证,特别注意中文问题!
fengliang2626 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 zhong_sen 的回复:]
我用php,做的也是遇见这样的问题,真是蛋疼啊...找了很多资料都没有解决,
[/Quote]
我也是用PHP+ajax实现局部刷新,XMLHttp.responseXML返回的是object,但xmlDoc.getelementsBytagname("info")返回的是0
zhong_sen 2010-11-24
  • 打赏
  • 举报
回复
我用php,做的也是遇见这样的问题,真是蛋疼啊...找了很多资料都没有解决,
「已注销」 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ghost_520 的回复:]
上网搜罗了一下 asp 的设置语法,貌似是在 getpersonalinfo.asp 前面加入 response.ContentType ="text/xml" 这据代码,你再试试吧。
[/Quote]

我用C# 做的也遇到这样的问题 通过以上方法已解决
zt0803 2010-03-19
  • 打赏
  • 举报
回复
我也出现了跟楼主同样的问题!望高手解决啊。
Kittyking 2009-10-22
  • 打赏
  • 举报
回复
我做了两天,也是这个问题,死也解决不出。结果发现是xml中的一个标签由于粗心大意没有写对。你的也应该是xml问题,可能有一点点小问题,没发现
theal 2009-10-13
  • 打赏
  • 举报
回复
回复一下看看高见
liooon 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 bxbacn 的回复:]
var xmlDoc=xmlHttp.responseXML.documentElement;
在这句下面,加上alert(xmlDoc)会弹出什么值
[/Quote]

提示:为空或者不是对象
bxbacn 2009-04-09
  • 打赏
  • 举报
回复
http://hi.baidu.com/bxba/blog/item/653362597dbd9b222834f0d6.html

我使用的ajax代码
bxbacn 2009-04-09
  • 打赏
  • 举报
回复
var xmlDoc=xmlHttp.responseXML.documentElement;
在这句下面,加上alert(xmlDoc)会弹出什么值
liooon 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 Ghost_520 的回复:]
引用 9 楼 Ghost_520 的回复:
response.contenttype="text/xml"


response.ContentType ="text/xml"

asp 里面有没有大小写的区别???


我用你的代码在 jsp 里面加上返回类型就可以了啊。
[/Quote]

asp 不区分大小写;你用前两个页面的代码吗?我想应该是getpersonalinfo.asp这个页面的问题;用alert输出responsetext,responseXML都有返回值,唯独responseXML.documentElement为NULL值.也许是getpersonalinfo.asp生成的XML不规范或者不是XML,但怎么看也看不出错在那.郁闷了~~

现在真在是无夸,只能先用responsetext来返回值,实现我的设计要求了.
Ghost_520 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Ghost_520 的回复:]
response.contenttype="text/xml"


response.ContentType ="text/xml"

asp 里面有没有大小写的区别???
[/Quote]

我用你的代码在 jsp 里面加上返回类型就可以了啊。
liooon 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 bxbacn 的回复:]
if(xmlHttp.readystate==4&&xmlHttp.status == 200) 当readystate状态为4的时候,status的返回值也有好几个的

status改成这个,另外对上面几个回答感到无语,ajax和asp等程序无关的,response.contenttype="text/xml"这个能加在asp中是行,但为什么不在js中指定这是一个xml文档呢

js中指定 xmlHttp.overrideMimeType('text/xml')
[/Quote]
回bxbacn :感谢你的回复,你的方法也试过,还是不行;哎,奇怪了.
bxbacn 2009-04-09
  • 打赏
  • 举报
回复
if(xmlHttp.readystate==4&&xmlHttp.status == 200) 当readystate状态为4的时候,status的返回值也有好几个的

status改成这个,另外对上面几个回答感到无语,ajax和asp等程序无关的,response.contenttype="text/xml"这个能加在asp中是行,但为什么不在js中指定这是一个xml文档呢

js中指定 xmlHttp.overrideMimeType('text/xml')
Ghost_520 2009-04-09
  • 打赏
  • 举报
回复
response.contenttype="text/xml"


response.ContentType ="text/xml"

asp 里面有没有大小写的区别???
liooon 2009-04-09
  • 打赏
  • 举报
回复
response.contenttype="text/xml"这句在我上面getpersonalinfo.asp里的代码有了,你说的我在网上都找过,也试过,但不是这个问题喔.
Ghost_520 2009-04-09
  • 打赏
  • 举报
回复

上网搜罗了一下 asp 的设置语法,貌似是在 getpersonalinfo.asp 前面加入 response.ContentType ="text/xml" 这据代码,你再试试吧。
Ghost_520 2009-04-09
  • 打赏
  • 举报
回复

我用 jsp 知道是什么原因的,原因是因为你的 getpersonalinfo.asp 页面没有设置返回的字符串是什么类型,一般返回的是 "text/html" 如果要解析成 xml

的话,这里要设置成 "text/xml" ,asp 语法不是很熟,不知道怎么设置,下面我贴出 jsp 设置的语法:


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml"); // 像这样设置返回的类型。

PrintWriter out = response.getWriter();
out.println("<?xml version='1.0' encoding='utf-8'?>");
out.println("<personalinfo>");
out.println("<name>aa</name>");
out.println("<division>bbbb</division>");
out.println("<dept>aaaa</dept>");
out.println("<extension>123234</extension>");
out.println("<email>liubin5893@126.com</email>");
out.println("</personalinfo>");

}
liooon 2009-04-09
  • 打赏
  • 举报
回复
难不成,要生成xml文件后,再加载分析;哎,网上能找的能都找了,都没有找到相似的问题,郁闷啊;再不行,打算放弃了,改用responsetext,取字符串分析了.
liooon 2009-04-09
  • 打赏
  • 举报
回复
alert(xmlHttp.responsetext)可以返回值,返回的是相应工号在数据库里查找到的值,这些值就是我想要的,不过现在问题是,想通过documentElement取得根节点,但用xmlHttp.responseXML.documentElement返回的就是NULL
加载更多回复(3)

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧