danwoods
danwoods

Reputation: 4907

xml validity, ajax, and php

I'm returning some XML from PHP to Javascript via ajax and getting some 'invalid xml' errors. The xml I'm returning looks like this:

<response>
<song>tdb2009-01-29s2s06</song>
<song>tdb2009-01-29s1s02</song>
</response>

And my javascript to parse it looks like:

 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:\n" + 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...

And I'm getting a

Error reading the response: Invalid XML Strucutre

error. Does all this look right to you? Is the xml wrong or is it being loaded wrong? All help is greatly appreciated. Thanks in advance...

Upvotes: 0

Views: 464

Answers (3)

Bruno Sim&#245;es
Bruno Sim&#245;es

Reputation: 721

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

Upvotes: 1

Jani Hartikainen
Jani Hartikainen

Reputation: 43243

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.

Upvotes: 0

Rik Heywood
Rik Heywood

Reputation: 13972

You may need to include an xml header...

<?xml version="1.0" ?> 

Upvotes: 0

Related Questions