// HTTP 204 does not contain a body, the `content-type` is undefined.
this._success(xhr, options);
} else {
- if (options.responseType && xhr.getResponseHeader("Content-Type")!.indexOf(options.responseType) !== 0) {
+ if (options.responseType && this.getContentType(xhr) !== options.responseType) {
// request succeeded but invalid response type
this._failure(xhr, options);
} else {
if (typeof options.success === "function") {
let data: ResponseData | null = null;
- if (xhr.getResponseHeader("Content-Type")!.split(";", 1)[0].trim() === "application/json") {
+ if (this.getContentType(xhr) === "application/json") {
try {
data = JSON.parse(xhr.responseText) as ResponseData;
} catch (e) {
}
});
}
+
+ private getContentType(xhr: XMLHttpRequest): string | null {
+ const contentType = xhr.getResponseHeader("content-type");
+ if (contentType === null) {
+ return null;
+ }
+
+ return contentType.split(";", 1)[0].trim();
+ }
}
Core.enableLegacyInheritance(AjaxRequest);
this._success(xhr, options);
}
else {
- if (options.responseType && xhr.getResponseHeader("Content-Type").indexOf(options.responseType) !== 0) {
+ if (options.responseType && this.getContentType(xhr) !== options.responseType) {
// request succeeded but invalid response type
this._failure(xhr, options);
}
}
if (typeof options.success === "function") {
let data = null;
- if (xhr.getResponseHeader("Content-Type").split(";", 1)[0].trim() === "application/json") {
+ if (this.getContentType(xhr) === "application/json") {
try {
data = JSON.parse(xhr.responseText);
}
}
});
}
+ getContentType(xhr) {
+ const contentType = xhr.getResponseHeader("content-type");
+ if (contentType === null) {
+ return null;
+ }
+ return contentType.split(";", 1)[0].trim();
+ }
}
Core.enableLegacyInheritance(AjaxRequest);
return AjaxRequest;