HTTP 204 does not carry a `content-type`
authorAlexander Ebert <ebert@woltlab.com>
Mon, 16 May 2022 09:17:50 +0000 (11:17 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 16 May 2022 09:17:50 +0000 (11:17 +0200)
See https://www.woltlab.com/community/thread/295631-importer-h%C3%A4ngt-bei-reaktionen-100/

ts/WoltLabSuite/Core/Ajax/Request.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Request.js

index 928010c51b23b18d236cbd26e42df55fc2cf4ff3..9b6d8d042070b40a516034f5ba6568eb2a181566 100644 (file)
@@ -140,11 +140,16 @@ class AjaxRequest {
     xhr.onload = () => {
       if (xhr.readyState === XMLHttpRequest.DONE) {
         if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
-          if (options.responseType && xhr.getResponseHeader("Content-Type")!.indexOf(options.responseType) !== 0) {
-            // request succeeded but invalid response type
-            this._failure(xhr, options);
-          } else {
+          if (xhr.status === 204) {
+            // 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) {
+              // request succeeded but invalid response type
+              this._failure(xhr, options);
+            } else {
+              this._success(xhr, options);
+            }
           }
         } else {
           this._failure(xhr, options);
@@ -250,7 +255,7 @@ class AjaxRequest {
         }
       }
 
-      options.success(data!, xhr.responseText, xhr, options.data!);
+      options.success(data || {}, xhr.responseText, xhr, options.data!);
     }
 
     this._finalize(options);
index 37da9d0885265cbefccc1c969113f4a6db1a5b40..ffdd084dfc32820d97e53e6ac2689852bf308eaa 100644 (file)
@@ -112,12 +112,18 @@ define(["require", "exports", "tslib", "./Status", "../Core", "../Dom/Change/Lis
             xhr.onload = () => {
                 if (xhr.readyState === XMLHttpRequest.DONE) {
                     if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
-                        if (options.responseType && xhr.getResponseHeader("Content-Type").indexOf(options.responseType) !== 0) {
-                            // request succeeded but invalid response type
-                            this._failure(xhr, options);
+                        if (xhr.status === 204) {
+                            // HTTP 204 does not contain a body, the `content-type` is undefined.
+                            this._success(xhr, options);
                         }
                         else {
-                            this._success(xhr, options);
+                            if (options.responseType && xhr.getResponseHeader("Content-Type").indexOf(options.responseType) !== 0) {
+                                // request succeeded but invalid response type
+                                this._failure(xhr, options);
+                            }
+                            else {
+                                this._success(xhr, options);
+                            }
                         }
                     }
                     else {
@@ -209,7 +215,7 @@ define(["require", "exports", "tslib", "./Status", "../Core", "../Dom/Change/Lis
                         void new Promise((resolve_1, reject_1) => { require(["../BackgroundQueue"], resolve_1, reject_1); }).then(tslib_1.__importStar).then((backgroundQueue) => backgroundQueue.invoke());
                     }
                 }
-                options.success(data, xhr.responseText, xhr, options.data);
+                options.success(data || {}, xhr.responseText, xhr, options.data);
             }
             this._finalize(options);
         }