Skip to content

Commit

Permalink
fix(ajax): non-json response bodies are discarded (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukhariev committed Feb 24, 2023
1 parent 0e06c76 commit 50d7c8d
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/uploadx/lib/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export class UploadxAjax {
xhr.open(method, url, true);
xhr.timeout = timeout;
withCredentials && (xhr.withCredentials = true);
responseType && (xhr.responseType = responseType);
if (responseType) {
xhr.responseType = responseType === 'json' ? 'text' : responseType;
}
Object.keys(headers).forEach(key => xhr.setRequestHeader(key, String(headers[key])));
xhr.upload.onprogress = onUploadProgress || null;
xhr.onerror =
Expand All @@ -62,7 +64,7 @@ export class UploadxAjax {
};
xhr.onload = () => {
const response = {
data: this.getResponseBody<T>(xhr),
data: this.getResponseBody<T>(xhr, responseType),
status: xhr.status,
headers: this.getResponseHeaders(xhr)
};
Expand All @@ -83,17 +85,15 @@ export class UploadxAjax {
}, {});
}

getResponseBody<T>(xhr: XMLHttpRequest): T {
if (xhr.responseType === 'document') {
return 'response' in xhr ? xhr.response : xhr.responseXML;
getResponseBody<T>(xhr: XMLHttpRequest, responseType?: string): T {
if (responseType === 'document') {
return typeof xhr.response === 'undefined' ? xhr.responseXML : xhr.response;
}
let body = 'response' in xhr ? xhr.response : xhr.responseText;
if (xhr.responseType === 'json') {
if (body && typeof body === 'string') {
try {
body = JSON.parse(body);
} catch {}
}
let body = typeof xhr.response === 'undefined' ? xhr.responseText : xhr.response;
if (responseType === 'json' && typeof body === 'string') {
try {
body = JSON.parse(body);
} catch {}
}
return body;
}
Expand Down

0 comments on commit 50d7c8d

Please sign in to comment.