Ajax: Don't throw exceptions on binary data response

Fixes gh-2498
Closes gh-2682

The added unit test shows how this could be used to support an
ArrayBuffer return, but $.ajax does not support it natively.
The goal with this change was to avoid the exception.
This commit is contained in:
Dave Methvin 2015-10-31 11:24:04 -04:00
parent 76e9a95dbe
commit 769446c697
2 changed files with 31 additions and 6 deletions

View File

@ -97,12 +97,13 @@ jQuery.ajaxTransport( function( options ) {
xhrSuccessStatus[ xhr.status ] || xhr.status, xhrSuccessStatus[ xhr.status ] || xhr.status,
xhr.statusText, xhr.statusText,
// Support: IE9 // Support: IE9 only
// Accessing binary-data responseText throws an exception // IE9 has no XHR2 but throws on binary (trac-11426)
// (#11426) // For XHR2 non-text, let the caller handle it (gh-2498)
typeof xhr.responseText === "string" ? { ( xhr.responseType || "text" ) !== "text" ||
text: xhr.responseText typeof xhr.responseText !== "string" ?
} : undefined, { binary: xhr.response } :
{ text: xhr.responseText },
xhr.getAllResponseHeaders() xhr.getAllResponseHeaders()
); );
} }

View File

@ -1660,6 +1660,30 @@ QUnit.module( "ajax", {
}; };
} ); } );
if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().responseType !== "string" ) {
QUnit.skip( "No ArrayBuffer support in XHR", jQuery.noop );
} else {
// No built-in support for binary data, but it's easy to add via a prefilter
jQuery.ajaxPrefilter( "arraybuffer", function ( s ) {
s.xhrFields = { responseType: "arraybuffer" };
s.responseFields.arraybuffer = "response";
s.converters[ "binary arraybuffer" ] = true;
});
ajaxTest( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception", 2, function( assert ) {
return {
url: url( "data/1x1.jpg" ),
dataType: "arraybuffer",
success: function( data, s, jqxhr ) {
assert.ok( data instanceof window.ArrayBuffer, "correct data type" );
assert.ok( jqxhr.response instanceof window.ArrayBuffer, "data in jQXHR" );
}
};
} );
}
QUnit.asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function( assert ) { QUnit.asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function( assert ) {
// Support: Android 2.3 only // Support: Android 2.3 only