日期:2014-05-16  浏览次数:23046 次

为什么取xmlHttp.responseXML.documentElement取为空? - Web 开发 / Ajax
要求,在asp页面中输入工号项中输入工号,自动在该页面中显示该工号的相关数据,比如对应的姓名,电话等.

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

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

.....
<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页面
JScript code

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)    
    {
        [color=#0000FF]var xmlDoc=xmlHttp.responseXML.documentElement;[/color]
        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页面
VBScript code

<%
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
%>