Skip to content

Commit 769446c

Browse files
committed
Ajax: Don't throw exceptions on binary data response
Fixes jquerygh-2498 Closes jquerygh-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.
1 parent 76e9a95 commit 769446c

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/ajax/xhr.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ jQuery.ajaxTransport( function( options ) {
9797
xhrSuccessStatus[ xhr.status ] || xhr.status,
9898
xhr.statusText,
9999

100-
// Support: IE9
101-
// Accessing binary-data responseText throws an exception
102-
// (#11426)
103-
typeof xhr.responseText === "string" ? {
104-
text: xhr.responseText
105-
} : undefined,
100+
// Support: IE9 only
101+
// IE9 has no XHR2 but throws on binary (trac-11426)
102+
// For XHR2 non-text, let the caller handle it (gh-2498)
103+
( xhr.responseType || "text" ) !== "text" ||
104+
typeof xhr.responseText !== "string" ?
105+
{ binary: xhr.response } :
106+
{ text: xhr.responseText },
106107
xhr.getAllResponseHeaders()
107108
);
108109
}

test/unit/ajax.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,30 @@ QUnit.module( "ajax", {
16601660
};
16611661
} );
16621662

1663+
if ( typeof window.ArrayBuffer === "undefined" || typeof new XMLHttpRequest().responseType !== "string" ) {
1664+
1665+
QUnit.skip( "No ArrayBuffer support in XHR", jQuery.noop );
1666+
} else {
1667+
1668+
// No built-in support for binary data, but it's easy to add via a prefilter
1669+
jQuery.ajaxPrefilter( "arraybuffer", function ( s ) {
1670+
s.xhrFields = { responseType: "arraybuffer" };
1671+
s.responseFields.arraybuffer = "response";
1672+
s.converters[ "binary arraybuffer" ] = true;
1673+
});
1674+
1675+
ajaxTest( "gh-2498 - jQuery.ajax() - binary data shouldn't throw an exception", 2, function( assert ) {
1676+
return {
1677+
url: url( "data/1x1.jpg" ),
1678+
dataType: "arraybuffer",
1679+
success: function( data, s, jqxhr ) {
1680+
assert.ok( data instanceof window.ArrayBuffer, "correct data type" );
1681+
assert.ok( jqxhr.response instanceof window.ArrayBuffer, "data in jQXHR" );
1682+
}
1683+
};
1684+
} );
1685+
}
1686+
16631687
QUnit.asyncTest( "#11743 - jQuery.ajax() - script, throws exception", 1, function( assert ) {
16641688

16651689
// Support: Android 2.3 only

0 commit comments

Comments
 (0)