Skip to content

Commit

Permalink
Fix exception when setting XHR responseType parameter
Browse files Browse the repository at this point in the history
According to the XHR spec (http://xhr.spec.whatwg.org/#the-responsetype-attribute) and
https://bugzilla.mozilla.org/show_bug.cgi?id=707484 it is legal to set the responseType attribute
before opening the request. However, this doesn't work with current versions of Firefox and results
in an exception.

Work around this issue by setting the xhr.responseType attribute only
after opening the request.

Also if the caller explicitly specifies a desired response type, report
an error instead of silently returning an unexpected data type if the
xhr.responseType assignment fails.

Fixes browserify#65
  • Loading branch information
robertknight committed Sep 6, 2014
1 parent ddeabab commit f5e5e9d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ var Request = module.exports = function (xhr, params) {
try { xhr.withCredentials = params.withCredentials }
catch (e) {}

if (params.responseType) try { xhr.responseType = params.responseType }
catch (e) {}

xhr.open(
params.method || 'GET',
self.uri,
true
);

if (params.responseType) {
try {
xhr.responseType = params.responseType
} catch (e) {
self.emit('error', e);
}
}

xhr.onerror = function(event) {
self.emit('error', new Error('Network error'));
};
Expand Down

0 comments on commit f5e5e9d

Please sign in to comment.