注册 登录
编程论坛 jQuery论坛

初学Ajax 的问题

ConZhang 发布于 2007-12-21 15:02, 1680 次点击
这几天因为项目的需要,老师让我学习 Ajax,于是找了本书《Ajax基础教程》,
找这里面的例子做,可是发现问题:
parseXML.html:
程序代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>Parsing XML Responses with the W3C DOM</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 (requestedList){
            requestType=requestedList;
            createXMLHttpRequest();
            xmlHttp.onreadystatechange=handleStateChange;
            xmlHttp.open("GET","parseXML.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(){
            var xmlDoc=xmlHttp.responseXML;
            var northNode=xmlDoc.getElementsByTagName("north")[0];
            var out="Northern States";
            var nothStates=northNode.getElementsByTagName('state');

            outputlist("Nothern States",northStates);
        }

        function listAllStates(){
            var xmlDoc=xmlHttp.responseXML;
            //alert(xmlHttp.responseXML.type);
            var allStates=xmlDoc.getElementsByTagName('state');
            alert(allStates.length);
            outputList("All States in Document:",allStates);
        }

        function outputList(title,states){
            var out=title;
            var currentState=null;
            alert(states.length);
            for(var i=0;i<states.length;i++){
                currentState=states[i];
                out=out+"\n"+currentState.childNodes[0].nodeValue;
            }
            alert(out);
        }
        </script>
    </head>
    <body>
        <h1>Process XML Document of U.S. States</h1>
        

        

        <form action="#">
            <input type="button" value="View All Listed States: " onclick="startRequest('all');"/>
            

            

            <input type="button" value="View All Listed Northern States" onclick="startRequest('north');"/>
        </form>
    </body>
</html>
parseXML.xml
程序代码:
<?xml version="1.0" encoding="UTF-8" ?>
<states>
    <north>
        <state>Minnesota</state>
        <state>Iowa</state>
        <state>North Dakota</state>
    </north>
    <south>
        <state>Texas</state>
        <state>OKlahoma</state>
        <state>Louisiana</state>
    </south>
    <east>
        <state>New York</state>
        <state>North Carolina</state>
        <state>Massachusetts</state>
    </east>
    <west>
        <state>California</state>
        <state>Oregon</state>
        <state>Nevada</state>
    </west>
</states>
到底怎么回事啊?
xmlHttp.responseXML返回列表的长度怎么是0啊?
求助
2 回复
#2
scdmike2007-12-22 10:52
你是怎么运行测试的啊?
#3
ConZhang2007-12-22 16:01
在这个listAllStates方法里面加了一句:alert(allStates.length);
然后然后运行是弹出的对话框显示:0
1