jQuery .find() doesn't return data in IE but does in Firefox and Chrome
Since IE's problem is its xml parser chokes on xml files that are not passed down using the correct "text/xml" header, you can include a bit of code in the Ajax complete event:
complete: function( xhr, status ) { alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ; if( status == 'parsererror' ) { alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" + "The complete server response text was " + xhr.responseText ) ; xmlDoc = null; // Create the xml document from the responseText string. // This uses the w3schools method. // see also if( window.DOMParser ) { parser=new DOMParser(); xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ; } else // Internet Explorer { xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ; xmlDoc.async = "false" ; xmlDoc.loadXML( xhr.responseText ) ; } $( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ; $( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ; processXMLDoc( xmlDoc ) ; } },
here's a more complete example
<!DOCTYPE html> <html> <head> <title>Reading XML with jQuery</title> <style> #response { border: solid 1px black; padding: 5px; } </style> <script src="jquery-1.3.2.min.js"></script> <script> function processXMLDoc( xmlDoc ) { var heading = $(xmlDoc).find('heading').text() ; $( '#response' ).append( '<h1>' + heading + '</h1>' ) ; var bodyText = $(xmlDoc).find('body').text() ; $( '#response' ).append( '<p>' + bodyText + '</p>' ) ; } $(document).ready(function() { jQuery.ajax({ type: "GET", url: "a.xml", // ! watch out for same // origin type problems dataType: "xml", // 'xml' passes it through the browser's xml parser success: function( xmlDoc, status ) { // The SUCCESS EVENT means that the xml document // came down from the server AND got parsed successfully // using the browser's own xml parsing caps. processXMLDoc( xmlDoc ); // IE gets very upset when // the mime-type of the document that // gets passed down isn't text/xml. // If you are missing the text/xml header // apparently the xml parse fails, // and in IE you don't get to execute this function AT ALL. }, complete: function( xhr, status ) { alert( "COMPLETE. You got:\n\n" + xhr.responseText ) ; if( status == 'parsererror' ) { alert( "There was a PARSERERROR. Luckily, we know how to fix that.\n\n" + "The complete server response text was " + xhr.responseText ) ; xmlDoc = null; // Create the xml document from the responseText string. // This uses the w3schools method. // see also if( window.DOMParser ) { parser=new DOMParser(); xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ; } else // Internet Explorer { xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ; xmlDoc.async = "false" ; xmlDoc.loadXML( xhr.responseText ) ; } $( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ; $( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ; processXMLDoc( xmlDoc ) ; } }, error: function( xhr, status, error ) { alert( 'ERROR: ' + status ) ; alert( xhr.responseText ) ; } }); }); </script> </head> <body> <div> <h1><a href="http://think2loud.com/reading-xml-with-jquery/">Reading XML with jQuery</a></h1> <p> <a href="http://docs.jquery.com/Ajax/jQuery.ajax#options">#1 jQuery.ajax ref</a> </p> </div> <p>Server says:</p> <pre id="response"> </pre> </body> </html>
contents of a.xml
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
It extends this example.
Check the content type of the response. If you get messages.xml as the wrong mime type, Internet Explorer won't parse it as XML.
To check the content type, you need access to the XMLHttpRequest object. The normal success callback doesn't pass it as a parameter, so you need to add a generic ajaxComplete or ajaxSuccess event handler. The second parameter for those events is the XMLHttpRequest object. You can call the getResponseHeader method on it to get the content type.
$(document).ajaxComplete(function(e, x) {
alert(x.getResponseHeader("Content-Type"));
});
Unfortunately there's no way that I know of in Internet Explorer to override what the server sends, so if it's wrong you need to change the server to send "text/xml" for the content type.
Some browsers have a overrideMimeType
method that you can call before send
to force it to use "text/xml", but Internet Explorer doesn't support that as far as I know.