<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函数测试了,居然无法弹出对话框!请各位帮个忙!