24 March 2006

Getting responseXML to work in IE

Nightmare! This took me ages to solve, but was actually quite easy in the end.

I thought it'd be a great idea to have "live" validation on a registration page, that'd check the database to see if a username had already been chosen.

First I created a ColdFusion Component (CFC) as a web service which checks the user input against usernames in the database and returns a boolean true/false. The CFC returns a WDDX formatted XML packet containing the boolean value.

Assuming you have a response handler like the AJAX tutorial here: http://developer.mozilla.org/en/docs/AJAX, all you need to do is use http_request.responseXML to receive the XML content and then use the XML DOM properties and methods to extract the data.

This works fine in Mozilla browsers like FireFox / Safari, but not in IE, which produces an 'Object Expected' error.

This is because IE expects all XML responses to have: Content-Type: 'text/xml' in the response header. However, ColdFusion broadcasts the WDDX packet as 'text/html', and therefore using responseXML in the javascript handler will not work, as IE doesn't acknowledge this as XML data.

The simple way to fix this is to create an XML DOM object in the Javascript response handler and use the responseText property, like so:

if(window.ActiveXObject){ // If IE Windows
var XMLdoc = new ActiveXObject("Microsoft.XMLDOM");
XMLdoc.loadXML(http_request.responseText);
} else {
var XMLdoc = http_request.responseXML;
}

14 comments:

Sergei Golubev said...

Dan, thank you very much for your article. I had the problem with parsing XML data retrieved, and didn't think that there could be a bug at IE on this. Thanks again! Best, Sergei.

Anonymous said...

thanks - this helped out a bunch!

Anonymous said...

Thank you sooooo much!! It works great!

Cheers,
Su

Anonymous said...

OMG..... thank you... .that took forever... friggin IE

Anonymous said...

You have no idea how long I was struggling with this. So glad I happened across your article. Thanks!

Anonymous said...

thank you very much!

Anonymous said...

And another big thanks! This was really slowing me down.

Anonymous said...

Thank you very much! This solution is great!

Anonymous said...

OMG, thank you!

Anonymous said...

Thanks a lot for your post!
Greets from Germany

Anonymous said...

Thank you so much!!!

That worked perfectly !!!!


Greetings from Brazil.

Unknown said...

Thanks you helped me a lot, thanks man.

Anonymous said...

This is very great thank a lots

Anonymous said...

Awesome! Thank you very much.