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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示