我通过Ajax将一些XML从PHP返回到Javascript,但得到“无效的XML”错误提示。返回的XML如下所示:
<response>
<song>tdb2009-01-29s2s06</song>
<song>tdb2009-01-29s1s02</song>
</response>
解析它的javascript如下所示:
function u_handleServerResponse(){
//pull xml from xml response
var xmlResponse = xmlHttp.responseXML;
//check to see if xml was pulled
if(!xmlResponse || !xmlResponse.documentElement){
throw("Invalid XML Structure:
" + xmlHttp.responseText);
}
//this is for catching errors with firefox
var rootNodeName = xmlResponse.documentElement.nodeName;
//check for errors
if(rootNodeName == "parsererror"){
throw("Invalid XML Strucutre");
}
//get the root
xmlRoot = xmlResponse.documentElement;
var songArray = xmlRoot.getElementsByTagName("song");
for(var i = 0; i < songArray.length; i++){
etc., etc...
得到的错误如下:
Error reading the response: Invalid XML Strucutre
你觉得这是对的吗?XML本身是错的还是加载错了?我们非常感谢所有的帮助。
You may need to include an xml header...
<?xml version="1.0" ?>
In addition to the XML header pointed out by rikh, you may need to declare the Content-Type header as text/xml for the responseXML to be correctly populated.
If you still have problems take a look at this article:
https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6141415.html
Regards,
Bruno