jquery ajax + xml + IE = hair loss
Related Categories: Web20, Microsoft, jQuery, Rants/Raves, Ajax, Coldfusion, Javascript
I've done ajax before with jQuery with good results on all browsers...until
I used an XML return with IE (all IE's that is).
When returning XML from a CFC (or any other server-side script), IE doesn't treat the XML document as, well, XML.
It actually barfs on it unless you tell it to load up the ActiveX control to parse your XML doc. (psst..Thanks Microsoft).
Just look at this:
$.ajax({
type: "POST",
url: "ajaxtest.cfc?method=getallblogs",
data: datastring,
success: function(data) {
var xml;
if ( $.browser.msie ) {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(data);
} else {
xml = data;
}
$(xml).find('blog').each(function(){
var id = $(this).attr('id');
var title = $(this).find('name').text();
var url = $(this).find('url').text();
$('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#blogInfo');
});
}
});
See that little blurb in the ajax call for "if ($.browser.msie)"? That's the call you'll need to do to make IE behave like 99% of the other browsers out there.
I've put together a short demo. I know the code is duplicated, but this is for demo purposes. Hope this helps you guys doing XML data returns.
Oh and once again "Thank you Microsoft" for making my web development career such a wild adventure.
XML data return demo
p.s. the data returned is from my blackberry bloggers site. If you're a blackberry fan, check it out (http://www.blackberrybloggers.org). It might look a bit familiar :)
