Bug Tracker

Ticket #4958 (closed bug: fixed)

Opened 5 years ago

Last modified 2 years ago

ajax methods fail in IE for content types ending in +xml

Reported by: jaffathecake Owned by:
Priority: low Milestone: 1.5
Component: ajax Version: 1.3.2
Keywords: xml,rss,ajaxrewrite Cc:
Blocking: Blocked by:

Description

IE doesn't understand content types like application/rss+xml, responseXML contains an empty xml document.

To recreate, try and load an RSS file using jquery.get in IE 6 / 7. Ensure the RSS file is served as application/rss+xml.

As a workaround, you can create a new ActiveXObject("Microsoft.XMLDOM") and feed it the responseText.

Attachments

ajaxxmlfix.patch Download (735 bytes) - added by jaffathecake 5 years ago.
Rough solution

Change History

Changed 5 years ago by jaffathecake

Rough solution

comment:1 Changed 5 years ago by jaffathecake

The patch above uses browser detection, not sure how you'd solve this via feature detection. Also, the bug doesn't occur in IE8, but giving it the fix does no harm.

comment:2 Changed 5 years ago by dmethvin

Closed dup #4975.

comment:3 Changed 5 years ago by zevspitz

You don't need to create a new XMLDOM object; you can use the existing one.
Also, As far as the browser-detection goes, why not just detect data.documentElement === null?

if ( xml ) {
    if (data.documentElement === null && data.parseError.errorCode === 0) {
        data.loadXML(xhr.responseText);
    }
    if ( data.documentElement.nodeName === 'parsererror' ) {
        jQuery.error('parsererror');
    }
}

There is an additional check, for the parseError.errorcode: If the responseText could not be parsed as XML the first time, there is no point in trying it agasin.

comment:4 Changed 5 years ago by zevspitz

This would also be relevant to #2949

comment:5 Changed 5 years ago by zevspitz

An additional note: for browsers which don't support parseError, an error will be trapped by the calling function

comment:6 Changed 4 years ago by addyosmani

#2109 is a duplicate of this ticket.

comment:7 Changed 4 years ago by SlexAxton

  • Priority changed from major to low
  • Status changed from new to open
  • Milestone changed from 1.4 to 1.5

comment:8 Changed 4 years ago by rwaldron

  • Keywords xml,rss,ajaxrewrite added; xml rss removed

comment:9 Changed 4 years ago by jaubourg

  • Status changed from open to closed
  • Resolution set to fixed

The new ajax implementation will do the parsing automatically.

comment:10 Changed 2 years ago by SeanH

This is not fixed correctly and it needs additional checks apart from documentElement to distinguish a HTML Dom object from an XML Dom object.

There is a bug in IE 10 (10.0.8400.0), the version included in the windows 8 preview release, that shows that the check was not adequate. In this release no correctly set xml mime type is parsed as xml, instead it is parsed as html. This results in all code relying on jQuery.ajax calls using the dataType xml and content type "text/xml” failing.

My suggestion is the following:

Modify the check for xml to (+-line7889)

if ( xml && xml.documentElement && !xml.forms/* #4958 */ ) {

HTML Dom will have .forms XML Dom will not.

Add an extra converter in jQuery.ajaxSetup for text -> xml. (+-line7771)

"text xml": function (text){
	var xml = jQuery.parseXML(text);
	return xml;
}

comment:11 Changed 2 years ago by jaubourg

@SeanH,

Have you tried to report the bug to the IE team? It's obviously a problem on their end and I'd like to see if we could have this fixed in IE10 before hacking in jQuery.

Also, I don't really see why you redefine the converter while it's already set to parseXML:  https://github.com/jquery/jquery/blob/master/src/ajax.js#L358

comment:12 Changed 2 years ago by SeanH

I am going to report the bug to the IE team as well, but since I have investigated this I decided to post here first. I still believe an extra check is required on that line of code regardless if the IE team fixes / doesn't fix the issue.

As to your second question I redefined it simply because I didn't see that code and our code was wasn't working still.

I have found out why it wasn't working. We redefined the parseXML function and the code uses your internal one instead of ours.

We have horrible browser detection code that ensures the xml object returned as the selectSingle node method. We had to extend this for IE 10. Our code heavily relies on this method and we have not written a replacement yet for when IE uses the DOMParser.

(function($) {
	
	$.parseXML = function( data ) 
	{
		
		var xml, tmp;
		try 
		{
			if (!($.browser.msie && ($.browser.version == 9 || $.browser.version == 10)) && window.DOMParser ) 
			{ // Standard
				tmp = new DOMParser();
				xml = tmp.parseFromString( data , "text/xml" );
			} 
			else 
			{ // IE
				xml = new ActiveXObject( "Microsoft.XMLDOM" );
				xml.async = "false";
				xml.preserveWhiteSpace = true;
				xml.loadXML( data );
			}
		} 
		catch( e ) 
		{
			xml = undefined;
		}
		if ( !xml || !xml.documentElement || xml.getElementsByTagName( "parsererror" ).length ) 
		{
			try
			{
				if (data.toString)
					jQuery.error( "Invalid XML: " + data );
			}
			catch (e)
			{
				jQuery.error('Invalid XML (cannot represent object as string).');
			}
		}
		return xml;

	};

})(jQuery);
Note: See TracTickets for help on using tickets.