| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1466 人关注过本帖
标题:[求助]ajax执行如下代码和书上说的不一样,是为什么呢
只看楼主 加入收藏
lihaoru
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2004-11-11
收藏
 问题点数:0 回复次数:4 
[求助]ajax执行如下代码和书上说的不一样,是为什么呢

这个是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>

搜索更多相关主题的帖子: ajax Microsoft 代码 xmlHttp 
2006-04-30 22:53
lihaoru
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2004-11-11
收藏
得分:0 

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

2006-04-30 22:56
lihaoru
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2004-11-11
收藏
得分:0 

请大家帮帮忙 谢谢

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

2006-04-30 22:57
无根泉
Rank: 2
等 级:新手上路
威 望:4
帖 子:853
专家分:0
注 册:2004-11-4
收藏
得分:0 
<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>

我很菜,但我很努力!
2006-05-03 18:02
无根泉
Rank: 2
等 级:新手上路
威 望:4
帖 子:853
专家分:0
注 册:2004-11-4
收藏
得分:0 
以下是引用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编辑过]


我很菜,但我很努力!
2006-05-03 18:07
快速回复:[求助]ajax执行如下代码和书上说的不一样,是为什么呢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053053 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved