注册 登录
编程论坛 jQuery论坛

[求助]ajax执行如下代码和书上说的不一样,是为什么呢

lihaoru 发布于 2006-04-30 22:53, 1466 次点击

这个是parseXML.html的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Parsing XML Response with W3C DOM</title>
</head>
<script language="javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
return;
}
}

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(requestType == "north") {
listNorthStates();
}
else if(requestType == "all") {
listAllStates();
}
}
}

function listNorthStates() {
var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];
alert(northNode);
var out = "Northern States";
alert(northNode.getElementsByTagName("state"));
var northStates = northNode.getElementsByTagName("state");
outputList(out, northStates);
}

function listAllStates() {
var xmlDoc = xmlHttp.responseXML;
var out = "All States";
var allStates = xmlDoc.getElementsByTagName("state")
outputList(out, allStates)
}

function outputList(title, states) {
var currentState = null;
for(var i=0; i<states.length; i++) {
currentState = state[i];
var out = title + "\n-" + currentState.childNodes[0].nodeValue;
}
alert(out);
}
</script>

<body>
<input name="test" type="button" value="View All Listed States" onClick="startRequest('all');">
</body>
</html>

这个是parseXML.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>a</state>
<state>b</state>
<state>c</state>
</north>
<west>
<state>d</state>
<state>e</state>
<state>f</state>
</west>
</states>

4 回复
#2
lihaoru2006-04-30 22:56

应该显示abcdef
可我在IE6里执行了以后的结果是undefined
请问是哪里写得不对了?

#3
lihaoru2006-04-30 22:57

请大家帮帮忙 谢谢

[此贴子已经被作者于2006-4-30 23:02:51编辑过]

#4
无根泉2006-05-03 18:02
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Parsing XML Response with W3C DOM</title>
</head>
<script language="javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
return;
}
}

function startRequest(requestedList) {
requestType = requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "http://localhost/ajax/parseXML.xml", true);
xmlHttp.send(null);
}

function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(requestType == "north") {
listNorthStates();
}
else if(requestType == "all") {
listAllStates();
}
}
}

function listNorthStates() {
var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];
alert(northNode);
var out = "Northern States";
alert(northNode.getElementsByTagName("state"));
var northStates = northNode.getElementsByTagName("state");
outputList(out, northStates);
}

function listAllStates() {
var xmlDoc = xmlHttp.responseXML;
var out = "All States";
var allStates = xmlDoc.getElementsByTagName("state")
outputList(out, allStates)
}

function outputList(title, states) {
var currentState = null;
var str=title;
for(var i=0; i<states.length; i++) {
currentState = states[i];
str +=currentState.childNodes[0].nodeValue+"\n-";
}
alert(str);
}
</script>

<body>
<input name="test" type="button" value="View All Listed States" onClick="startRequest('all');">
</body>
</html>
#5
无根泉2006-05-03 18:07
以下是引用lihaoru在2006-4-30 22:53:00的发言:

这个是parseXML.html的代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Parsing XML Response with W3C DOM</title>
</head>
<script language="javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest() {
if(window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else {
return;
}
}

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(requestType == "north") {
listNorthStates();
}
else if(requestType == "all") {
listAllStates();
}
}
}

function listNorthStates() {
var xmlDoc = xmlHttp.responseXML;
var northNode = xmlDoc.getElementsByTagName("north")[0];
alert(northNode);
var out = "Northern States";
alert(northNode.getElementsByTagName("state"));
var northStates = northNode.getElementsByTagName("state");
outputList(out, northStates);
}

function listAllStates() {
var xmlDoc = xmlHttp.responseXML;
var out = "All States";
var allStates = xmlDoc.getElementsByTagName("state")
outputList(out, allStates)
}

function outputList(title, states) {
var currentState = null;
var str=title;
for(var i=0; i<states.length; i++) {
currentState = states[i];
str+=currentState.childNodes[0].nodeValue+ "\n-" ;

}
alert(str);
}
</script>

<body>
<input name="test" type="button" value="View All Listed States" onClick="startRequest('all');">
</body>
</html>

这个是parseXML.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>a</state>
<state>b</state>
<state>c</state>
</north>
<west>
<state>d</state>
<state>e</state>
<state>f</state>
</west>
</states>


红色部分为添加的,蓝色部分为修改的!

[此贴子已经被作者于2006-5-3 18:07:32编辑过]

1