View Full Version : PHP to XHR XML issue

11-11-2010, 10:08 PM
Hi, I hope this is the right place for this query, because it relates to PHP and AJAX.

I am having a lot of trouble trying to get a PHP page to successfully communicate with an AJAX script through XML. After a lot of googling, I have come across the following example:


This example works well, and when I download it onto my own server it works as well. In principle my coding method is the same as the example, call the php file from XHR, generate XML header and echo body back to XHR, and then construct an array in Javascript based on the returned XML document's DOM. I have successfully tested that the returning header is text/xml, and when I insert my XML echo body into the example's php, it returns fine.

The problem is when I return the body to XHR in my page, the resulting array is empty, but only when I use responseXML. When I use responseText all the XML body is there. Out of desperation, I have made my XHR code identical to the example, and that still did not make the code work. So then I looked at my php, and it appears that I cannot have anything else active in my php (such as required files, functions, function calls, conditional structures etc). When my php file only contains the XML header and the echoed XML body, the XHR xmlDoc.getElementsByTagName array is populated. I have tried to enable parts of my php to see if there are conflicts but there are none that I can see, and whenever I enable even the most trivial thing (such a junk function that echoes a string) the XML is not received by XHR.

As I understand it, XHR should not be aware of goings on the server side until it responds anyway, so I dont see how include files et al should have an effect on the XML being sent. And also, the above example does have a conditional structure in the php before the XML body is echoed. Why is this allowed, and yet my php page has to be devoid of any other code? I have even tried making the XML header and body be echoed from an include file, but this failed to work as well. :mad:

If anyone can let me know why this is, and if I am missing something in my understanding of how PHP and AJAX/XHR should interact when using XML, I would be very grateful. :thumbsup:

rnd me
11-11-2010, 10:55 PM
The problem is when I return the body to XHR in my page, the resulting array is empty, but only when I use responseXML. When I use responseText all the XML body is there.

very simple problem.

you xml needs to be served as an xml mime type ("text/xml" is most common) to gain the responseXML property. If it's served at html, you can only get the responseText...

use php's header functions to change the mime (Content-Type) before you echo/print any xml.

11-12-2010, 02:01 AM
Hi, thanks for your reply, but I am already setting the header to text/xml in php with the following:

header('Cache-Control: no-cache');
header('Cache-Control: no-store' , false);

This is being received by the client side because when I use
getAllResponseHeaders() XHR returns text/xml. Also responseXML works when the php contains nothing other than the header and the echoed xml body. But when I add a single line of php code other than that, responseXML is null.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum