Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use try/catch when setting xhr.responseType #592

Merged
merged 1 commit into from
Feb 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/transports/polling-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ Request.prototype.create = function () {
xhr.setRequestHeader('Accept', '*/*');
} catch (e) {}

if (this.supportsBinary) {
xhr.responseType = 'arraybuffer';
}

// ie6 check
if ('withCredentials' in xhr) {
xhr.withCredentials = true;
Expand All @@ -239,13 +243,12 @@ Request.prototype.create = function () {
} else {
xhr.onreadystatechange = function () {
if (xhr.readyState === 2) {
var contentType;
try {
contentType = xhr.getResponseHeader('Content-Type');
var contentType = xhr.getResponseHeader('Content-Type');
if (contentType !== 'application/octet-stream') {
xhr.responseType = 'text';
}
} catch (e) {}
if (contentType === 'application/octet-stream') {
xhr.responseType = 'arraybuffer';
}
}
if (4 !== xhr.readyState) return;
if (200 === xhr.status || 1223 === xhr.status) {
Expand Down Expand Up @@ -355,7 +358,11 @@ Request.prototype.onLoad = function () {
contentType = this.xhr.getResponseHeader('Content-Type');
} catch (e) {}
if (contentType === 'application/octet-stream') {
data = this.xhr.response || this.xhr.responseText;
if (this.xhr.responseType === 'arraybuffer') {
data = this.xhr.response || this.xhr.responseText;
} else {
data = String.fromCharCode.apply(null, new Uint8Array(this.xhr.response));
}
} else {
data = this.xhr.responseText;
}
Expand Down