From 5fdbddf5de03d9fc8ee305ef88440b028bcee3da Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 3 Nov 2022 17:57:13 +0100 Subject: [PATCH] Add support for `FormData` and JSON body --- ts/WoltLabSuite/Core/Ajax/Backend.ts | 11 ++++++++--- .../files/js/WoltLabSuite/Core/Ajax/Backend.js | 10 ++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ts/WoltLabSuite/Core/Ajax/Backend.ts b/ts/WoltLabSuite/Core/Ajax/Backend.ts index 1fd167d9cb..70de0ba8c2 100644 --- a/ts/WoltLabSuite/Core/Ajax/Backend.ts +++ b/ts/WoltLabSuite/Core/Ajax/Backend.ts @@ -15,7 +15,7 @@ const enum RequestType { POST, } -type Payload = Record; +type Payload = FormData | Record; 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"; diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Backend.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Backend.js index ed528d7800..374b853e23 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Backend.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ajax/Backend.js @@ -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 { -- 2.20.1