responseXML not working in IE

I was attempting to use Ajax.Requst object in Prototype.js to parse a static xml file, which worked in Firefox 2 but not IE 6 on PC. It turns out that in IE, the request returns responseText instead of responseXML. And it's due to my web host doesn't set Content-Type HTTP header for static file as application/xml,etc. (when served through php, you can set document header dynamically.)

I found out one discussion about the same issue here. People were trying to figure out a client side solution and most of the them mentioned using a hidden iframe as an intermediate holder and writing custom function to parse the responseText into the iframe.

http://www.quirksmode.org/blog/archives/2005/09/xmlhttp_notes_r_1.html

and

http://forums.devshed.com/javascript-development-115/prototype-returning-xml-379021.html

Solutions:

Server-side:

We need to set the MIME type to ‘text/xml’ or ‘application/xml’, like so:

/**php*/

header("Content-type: text/xml");
or
header("Content-type: application/xml");

Then print out the xml nodes as strings. The whole sample code is provided as follows:

<?php
//header("Content-type: text/xml");
header("Content-type: application/xml");
$xml_output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
$xml_output .= "<donors>";
$xml_output .= "<donor>Steven Spielberg</donor>";
$xml_output .= "<donor/>";
$xml_output .= "<donor/>";
$xml_output .= "</donors>";
echo $xml_output;
?>

Client side

Since currently no main stream browsers are supporting E4X, and parsing XML<->native JavaScript object involve overheads both physically (processing) and mentally (learning curve for something too small to be worth it), we will just use the old school JavaScript + Prototype Ajax.Request object. Like so:

function initApp()
{
new Ajax.Request(url, {
method: 'get',
onSuccess: function(req){
onResults(req);
}
}
);
}

function onResults(req)
{
//Render data
var info = req.responseXML;
var infoIE = req.responseXML.documentElement;
alert('results returned>>>>>>> ' + infoIE.firstChild.childNodes[0].nodeValue);
}

initApp();

Note that req.responseXML only works at FF but not IE while req.responseXML.documentElement works fine both at IE and FF.

Advertisements

6 Responses to “responseXML not working in IE”

  1. Sask Says:

    Thanks heaps – this helped me find a bug in my application, I was forgetting to set the content type. Was stumped that it was working in FF and not IE

  2. Says:

    Also worth noting that you can override the mime type manually on the client side of your ajax application using;

    req.overrideMimeType(‘text/html’);

  3. Says:

    hrm, ignore that comment… only works in FF.

    Well, still worth-while for people that don’t know how to change mime type on the client side for FF.

  4. Jeff Says:

    You can also do the following to use PHP to serve a static XML file:

    <?php
    header(”Content-type: application/xml”);
    echo ”;
    ?>

    Steven Spielberg

    That is much easier on the PHP parser, it is more efficient, and it is a lot easier to edit the XML file as it is almost a real XML file with just the tag at the top. Sadly, it is likely to not validate as XML as it is, although little modification is necessary to make it validate.

  5. Jeff Says:

    Well all the tags disapeared, here is the code with [] instead of
    [?php
    header(“Content-type: application/xml”);
    echo ‘[?xml version=”1.0″ encoding=”utf-8″?]’;
    ?]
    [donors]
    [donor]Steven Spielberg[/donor]
    [donor/]
    [donor/]
    [/donors]

  6. Solution Rally Says:

    responseXML still not working in IE9.
    It isays xmlhttprequest doesn’t contain any such property or method.
    How do solve it?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: