Add support for `FormData` and JSON body
authorAlexander Ebert <ebert@woltlab.com>
Thu, 3 Nov 2022 16:57:13 +0000 (17:57 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 8 Dec 2022 12:33:08 +0000 (13:33 +0100)
ts/WoltLabSuite/Core/Ajax/Backend.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Backend.js

index 1fd167d9cbee71ff450239fab4d945c14374d39e..70de0ba8c2fd5c61ce86419beb958cca3d79f710 100644 (file)
@@ -15,7 +15,7 @@ const enum RequestType {
   POST,
 }
 
-type Payload = Record<string, unknown>;
+type Payload = FormData | Record<string, unknown>;
 
 class SetupRequest {
   private readonly url: string;
@@ -79,10 +79,15 @@ class BackendRequest {
 
     if (this.#type === RequestType.POST) {
       init.method = "POST";
-      init.headers!["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
 
       if (this.#payload) {
-        init.body = JSON.stringify(this.#payload);
+        if (this.#payload instanceof FormData) {
+          init.headers!["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
+          init.body = this.#payload;
+        } else {
+          init.headers!["Content-Type"] = "application/json; charset=UTF-8";
+          init.body = JSON.stringify(this.#payload);
+        }
       }
     } else {
       init.method = "GET";
index ed528d78005f848d5668f20783035f5bd765514d..374b853e233da5ea5b51282618117054bb3256e7 100644 (file)
@@ -52,9 +52,15 @@ define(["require", "exports", "tslib", "./Status", "./Error", "../Core"], functi
             };
             if (this.#type === 1 /* RequestType.POST */) {
                 init.method = "POST";
-                init.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
                 if (this.#payload) {
-                    init.body = JSON.stringify(this.#payload);
+                    if (this.#payload instanceof FormData) {
+                        init.headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
+                        init.body = this.#payload;
+                    }
+                    else {
+                        init.headers["Content-Type"] = "application/json; charset=UTF-8";
+                        init.body = JSON.stringify(this.#payload);
+                    }
                 }
             }
             else {