jiangui1988 2010-07-20 11:27
浏览 244
已采纳

做AJAXDemo的时候总是在<input>行报缺少对象错误,调试了半天也没有出来?

 

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>this is a test for using XML to understand the way how to use AJAX</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","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 northStates = northNode.getElementsByTagName("state");
            outputList(out, northStates);
        }
        
        function listAllStates() {
            var xmlDoc = xmlHttp.responseXML;
            var allStates = xmlDoc.getElementsByTagName("state");
            
            outputList("All State In Document", allStates);
        }
        
        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;
            }
            alert(out);
        }
    </script>
  </head>
  
  <body>
    <h1>Process XML Document of U.S States </h1>
    <br/><br/>
    <form action="#">
        <input type="button" value="view all listed states" onclick="startRequest('all');" />
        <br/><br/>
        <input type="button" value="view all listed northern states" onclick="startRequest('north');" />
    </form>
  </body>
</html>

这段代码在运行的时候总是在下面两个<input>处报错:缺少对象!用alert函数测试了,居然无法弹出对话框!请各位帮个忙!

  • 写回答

2条回答 默认 最新

  • loveyou0406 2010-07-20 13:42
    关注

    将代码
    [code="java"] function handleStateChange() {

    if(xmlHttp.readyState == 4) {

    if(xmlHttp.status == 200) {

    if(requestType == "north") {

    listNorthStates();

    }

    else if(requestType == "all") {

    listAllStates();

    }

    )

    )

    } [/code]
    修改成
    [code="java"]
    function handleStateChange() {

    if(xmlHttp.readyState == 4) {

    if(xmlHttp.status == 200) {

    if(requestType == "north") {

    listNorthStates();

    }

    else if(requestType == "all") {

    listAllStates();

    }

    }

    }

    } [/code]
    再试一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题