Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    United Kingdom
    Posts
    27
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Question PHP to XHR XML issue

    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:

    http://in3d.eu/in3d_Programming/AJAX...sponseXML.html

    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.

    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.
    Attached Files Attached Files

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Quote Originally Posted by tr_y2k View Post
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New Coder
    Join Date
    Mar 2005
    Location
    United Kingdom
    Posts
    27
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi, thanks for your reply, but I am already setting the header to text/xml in php with the following:

    Code:
    header("Content-Type:text/xml");
    header('Cache-Control: no-cache');
    header('Cache-Control: no-store' , false);
    This is being received by the client side because when I use
    Code:
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •