请问为何ajax返回的XML用getElementsByTagName取不到值
请教为何ajax返回的XML用getElementsByTagName取不到值
请教为何ajax返回的XML用getElementsByTagName取不到值
临时做了一个的PHP页面,希望返回一个xml
<?php
header("Content-Type:text/xml;charset=utf-8");
header("cache-control:no-cache,must-revalidate");
$xml="<?xml version='1.0' encoding='gb2312'?>";
$xml.="<ebook>";
$tt="<book>aa</book>";
$xml.=$tt."</ebook>";
echo $xml;
?>
function getrequest()
{
var xmlhttp=null;
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlhttp=false;
}
}
}
return xmlhttp;
}
var myxmlHttp;
//留言
function liuyan()
{
myxmlHttp=getrequest();
if(myxmlHttp)
{
var title=document.getElementById('title').value;
var content=document.getElementById('content').value;
var author=document.getElementById('author').innerHTML;
var e_id=document.getElementById('t_id').value;
var str="title="+title+"&content="+content+"&author="+author+"&e_id="+e_id;
alert(str);
var url="http://localhost/book/article_proc.php";
myxmlHttp.open("post",url,true);
myxmlHttp.onreadystatechange=callback;
myxmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
myxmlHttp.send(str);
}
}
function callback()
{
if(myxmlHttp.readyState==4)
{
show();
// alert(myxmlHttp.responseText);
}
}
//但在下面alert(xmlDoc)能够正确显示出“<ebook><book>bb</book></ebook>”
//而alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);却什么也没有,请问是哪里出了问题?新人求助
function show()
{
var xmlDoc=myxmlHttp.responseXML.xml;
alert(xmlDoc);
alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);
}
------解决方案--------------------
xmlDoc=myxmlHttp.responseXML 得到的是 domxml 对象
xmlDoc=myxmlHttp.responseXML.xml 达到的 domxml 对象的文本化串,与 myxmlHttp.responseText 是一样的
所以你应该
xmlDoc=myxmlHttp.responseXML;
alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);
请教为何ajax返回的XML用getElementsByTagName取不到值
临时做了一个的PHP页面,希望返回一个xml
<?php
header("Content-Type:text/xml;charset=utf-8");
header("cache-control:no-cache,must-revalidate");
$xml="<?xml version='1.0' encoding='gb2312'?>";
$xml.="<ebook>";
$tt="<book>aa</book>";
$xml.=$tt."</ebook>";
echo $xml;
?>
function getrequest()
{
var xmlhttp=null;
try
{
xmlhttp=new XMLHttpRequest();
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlhttp=false;
}
}
}
return xmlhttp;
}
var myxmlHttp;
//留言
function liuyan()
{
myxmlHttp=getrequest();
if(myxmlHttp)
{
var title=document.getElementById('title').value;
var content=document.getElementById('content').value;
var author=document.getElementById('author').innerHTML;
var e_id=document.getElementById('t_id').value;
var str="title="+title+"&content="+content+"&author="+author+"&e_id="+e_id;
alert(str);
var url="http://localhost/book/article_proc.php";
myxmlHttp.open("post",url,true);
myxmlHttp.onreadystatechange=callback;
myxmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
myxmlHttp.send(str);
}
}
function callback()
{
if(myxmlHttp.readyState==4)
{
show();
// alert(myxmlHttp.responseText);
}
}
//但在下面alert(xmlDoc)能够正确显示出“<ebook><book>bb</book></ebook>”
//而alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);却什么也没有,请问是哪里出了问题?新人求助
function show()
{
var xmlDoc=myxmlHttp.responseXML.xml;
alert(xmlDoc);
alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);
}
------解决方案--------------------
xmlDoc=myxmlHttp.responseXML 得到的是 domxml 对象
xmlDoc=myxmlHttp.responseXML.xml 达到的 domxml 对象的文本化串,与 myxmlHttp.responseText 是一样的
所以你应该
xmlDoc=myxmlHttp.responseXML;
alert(xmlDoc.getElementsByTagName("book")[0].childNodes[0].firstChild.data);