5个QQ群里没人解决的Ajax小问题:<ajax基础教程>上的一段代码,请指教~~!
一个静态parseXML.xml文件:
<?xml version= "1.0 " encoding= "UTF-8 "?>
<states>
<north>
<state> jilin </state>
<state> heilongjiang </state>
<state> haerbin </state>
</north>
<south>
<state> gz </state>
<state> sz </state>
<state> nn </state>
</south>
<east>
<state> nj </state>
<state> sh </state>
<state> nb </state>
</east>
<west>
<state> qh </state>
<state> wh </state>
<state> kkxl </state>
</west>
</states>
HTML文件里两个Button:
<input name= "northButton " type= "button " value= "显示北部城市 " onclick= "startRequest( 'north ') " />
<input name= "allButton " type= "button " value= "显示全部城市 " onclick= "startRequest( 'all ') " />
HTML文件里的JS:
<script type= "text/javascript ">
var xmlHttp;
var requestType;
function createHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new window.ActiveXObject( "Microsoft.XMLHTTP ");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function startRequest(requestList)
{
requestType=requestList;
createHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open( "GET ", "parseXML.xml ",true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readystate==4)
{
//本机返回成功时的xmlHttp.status值也为0,所以下一句判断status是否为0
if(xmlHttp.status==200||xmlHttp.status==0)
{
alert(xmlHttp.responseText);
if(requestType== "north ")
{
ListNorthStates();
}
else if(requestType== "all ")
{
ListAllStates();
}
}
}
}
function ListNorthStates()
{
var xmlDoc=xmlHttp.responseXML;
//如果用xmlHttp.responseText,则可以得到正常的XML文件
var northNode=xmlDoc.getElementsByTagName( "north ")[0];
var states=northNode.getElementsByTagName( "state ");
alert(states.length);//这里得出的states.length既然是0
var out= "north States ";
outPutList(out,states);
}
function ListAllStates()
{
var xmlDoc=xmlHttp.responseXML;
var states=xmlDoc.getElementsByTagName( "state ");
alert(states.length);//这里得出的states.length也是0
var out= "all States ";
outPutList(out,states);
}
function outPutList(title,states)
{
var out=title;
var currenState=null;
for(var i=0;i < states.length;i++)
{
currenState=states[i];
out=out+ "\n- "+currenState.childNodes[0].nodeValue;
}
alert(out);
}
</script>
------------------------------------------
问题:只要把xmlHttp.open的URL设置为远程的XML文件,访问