做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个回答

将代码
[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]
再试一下

有区别的 你两个小括号修改成两个大括号了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐