Ajax中解析xml时用xmlHttp.responseXML.xml返回为空
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xh...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple</title>
<script type="text/javascript">
var xmlHttp = false;
var requestType="";
function createXMLHttpRequest(){
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = false;
}
}
}
if (!xmlHttp)
alert("Error initializing XMLHttpRequest!");
}
function startRequest(requestedList){
requestType=requestedList;
createXMLHttpRequest();
var url = "parseXML.xml";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==0){
if(requestType=="north")
{
listNorthStates();
}
else if(requestType=="all")
{
listAllStates();
}
//document.getElementById("results").innerHTML=xmlHttp.responseText;
}else
alert("status is " + xmlHttp.status);
}
}
function listNorthStates(){
var xmlDoc=xmlHttp.responseXML;
var northNode=xmlDoc.getElementsByTagName("north")[0];
var northStates=northNode.getElementsByTagName("state");
outputList("Northern States",northStates);
}
function listAllStates(){
var xmlDoc=xmlHttp.responseXML;
var allStates=xmlDoc.getElementsByTagName("states");
alert(xmlHttp.responseText);
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>
<h1>process xml document of us 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 northen states" onclick="startRequest('north');"/>
</form>
<div id="results"></div>
</body>
</html>
程序代码如上,怎么就不能返回正确的xml呢
不是xmlHttp.status的问题,这里设置为0是可以的 展开
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple</title>
<script type="text/javascript">
var xmlHttp = false;
var requestType="";
function createXMLHttpRequest(){
try {
xmlHttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = false;
}
}
}
if (!xmlHttp)
alert("Error initializing XMLHttpRequest!");
}
function startRequest(requestedList){
requestType=requestedList;
createXMLHttpRequest();
var url = "parseXML.xml";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==0){
if(requestType=="north")
{
listNorthStates();
}
else if(requestType=="all")
{
listAllStates();
}
//document.getElementById("results").innerHTML=xmlHttp.responseText;
}else
alert("status is " + xmlHttp.status);
}
}
function listNorthStates(){
var xmlDoc=xmlHttp.responseXML;
var northNode=xmlDoc.getElementsByTagName("north")[0];
var northStates=northNode.getElementsByTagName("state");
outputList("Northern States",northStates);
}
function listAllStates(){
var xmlDoc=xmlHttp.responseXML;
var allStates=xmlDoc.getElementsByTagName("states");
alert(xmlHttp.responseText);
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>
<h1>process xml document of us 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 northen states" onclick="startRequest('north');"/>
</form>
<div id="results"></div>
</body>
</html>
程序代码如上,怎么就不能返回正确的xml呢
不是xmlHttp.status的问题,这里设置为0是可以的 展开
2个回答
展开全部
xmlHttp.status==200 试试
那你有没有进行数据传递跟踪?跟踪到哪个地方数据才开始丢失的?总不会无缘无故的丢失吧,或许是服务端的问题呢,是不是传输模式没有调?
那你有没有进行数据传递跟踪?跟踪到哪个地方数据才开始丢失的?总不会无缘无故的丢失吧,或许是服务端的问题呢,是不是传输模式没有调?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xmlHttp.status==500 试试
如果不行就算了。
如果不行就算了。
参考资料: http://www.iamlk.cn/yufa/sitemap.xml
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询