<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Ajax_ParseXML.aspx.cs" Inherits="AllDemos.Ajax_ParseXML" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var xmlHttp;
var requestType='qwe';
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function startRequest(requestList){
//alert('startRequest...');
requsetType=requestList;
alert(requsetType);
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
alert(requsetType);
xmlHttp.open("GET","Ajax_ParseXML.xml",true);
xmlHttp.send(null);
}
function handleStateChange(){
alert(requestType);
if(xmlHttp.readyState==4)
if(xmlHttp.status==200){
if(requestType=="north")
listNorthStates();
else if(requestType=="all")
listAllStates();
}
}
function listNorthStates(){
//alert('listNorthStates...');
var xmlDoc=xmlHttp.responseXML;
var northNode=xmlDoc.getElementsByTagName("north")[0];
var out ="Northern States";
var northStates=northNode.getElementsByTagName("state");
outputList("Northern States",northStates);
}
function listAllStates(){
//alert('listAllStates...');
var xmlDoc=xmlHttp.responseXML;
var allStates=xmlDoc.getElementsByTagName("state");
outputList("All States 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>
<form id="form1" runat="server">
<div>
<h1>Proecss XML Document of U.S States</h1>
<input type="button" value="View All Listed States" onclick="startRequest('all')" />
<input type="button" value="View All Listed Northern States" onclick="startRequest('north')" />
</div>
</form>
</body>
</html>
function handleStateChange(){
alert(requestType); if(xmlHttp.readyState==4)
if(xmlHttp.status==200){
if(requestType=="north")
listNorthStates();
else if(requestType=="all")
listAllStates();
}
}
主要问题是在requestType,在startRequest(requestList)函数中已经赋值了,但是到handleStateChange()函数中时,值又变为初始值了!全局变量啊!!!页面不刷新!!!跪求问题了!
var xmlHttp;
//var requestType='qwe';
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp=new XMLHttpRequest();
}
function startRequest(requestList){
//alert('startRequest...');
var requsetType=requestList; createXMLHttpRequest();
xmlHttp.open("GET","Ajax_ParseXML.xml"+new Date().getTime(),true);
xmlHttp.onreadystatechange=handleStateChange(requsetType);
alert(requsetType);
xmlHttp.send(null);
}
function handleStateChange(requestType){
alert(requestType);
if(xmlHttp.readyState==4)
if(xmlHttp.status==200){
if(requestType=="north")
listNorthStates();
else if(requestType=="all")
listAllStates();
}
}
function listNorthStates(){
//alert('listNorthStates...');
var xmlDoc=xmlHttp.responseXML;
var northNode=xmlDoc.getElementsByTagName("north")[0];
var out ="Northern States";
var northStates=northNode.getElementsByTagName("state");
outputList("Northern States",northStates);
}
function listAllStates(){
//alert('listAllStates...');
var xmlDoc=xmlHttp.responseXML;
var allStates=xmlDoc.getElementsByTagName("state");
outputList("All States 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>
全局变量是
requestType
而在startRequest函数里写的是
requsetType
s和e 位置反了,所以在startRequest里面只改变一个局部变量而已