急!刚学习ajax,在访问xml文档时出现xmlHttp.readystate为空或不是对象错误。
问题:想使用ajax功能实现访问XML文档,取出文档中的部分数据
错误信息:xmlHttp.readystate为空或不是对象
补充:在访问文本文件时没有出现错误。
请大家帮忙!谢谢
USstates.xml文档,表示美国州的名字并按方位分类了,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<states>
<north>
<state>Minnesota</state>
<state>Iowa</state>
<state>North</state>
</north>
<east>
<state>New York</state>
<state>North Carolina</state>
<state>Massachusetts</state>
</east>
<south>
<state>Texas</state>
<state>OKlahoma</state>
<state>Louisiana</state>
</south>
<west>
<state>Calinfornia</state>
<state>Oregon</state>
<state>Nevada</state>
</west>
</states>
想通过点击htm页中的按钮显示north方向国家的名称:
<!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>
<title>练习</title>
<script type ="text/javascript" >
var xmlHttp;
var requestType;
function createXmlHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(requestList)
{
requestType = requestList;
createXmlHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("Get","USstates.xml",true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readystate == 4)
{
if(xmlHttp.status == 200)
{
if(requestType=="north")
{
listNorthStates();
}
else if(requestType=="all")
{
listAllStates();
}
}
}
}
function listNorthStates()//显示north部分的国家
{
var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementByTagName("north")[0];
var out = "Northern States";
var northStates = northNode.getElementByTagName("state")[0];
outputList(out,northStates);
}
function outputList(title,states)
{
var out = title;
var currentState = null;
for(var i = 0;i < states.Length;i++)
{
currentState = states[i];
out = out + "/n- " + currentState.childNodes[0].nodeValue;
}
window.alert(out);
}
</script>
</head>
<body style="text-align: center">
<h2>Process XML Document Of