From 99703396f9e4e60902a8ab735a59010ac133576a Mon Sep 17 00:00:00 2001 From: jingwenzheng Date: Mon, 17 Oct 2022 14:22:31 +0800 Subject: [PATCH] fix: exception to sending formdata in webworker --- lib/adapters/xhr.js | 2 +- lib/platform/browser/index.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/adapters/xhr.js b/lib/adapters/xhr.js index e0233f5e01..8dc8f78039 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -58,7 +58,7 @@ export default function xhrAdapter(config) { } } - if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) { + if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) { requestHeaders.setContentType(false); // Let the browser set it } diff --git a/lib/platform/browser/index.js b/lib/platform/browser/index.js index 80284eb208..4ba2c7dbfa 100644 --- a/lib/platform/browser/index.js +++ b/lib/platform/browser/index.js @@ -31,6 +31,24 @@ const isStandardBrowserEnv = (() => { return typeof window !== 'undefined' && typeof document !== 'undefined'; })(); +/** + * Determine if we're running in a standard browser webWorker environment + * + * Although the `isStandardBrowserEnv` method indicates that + * `allows axios to run in a web worker`, the WebWorker will still be + * filtered out due to its judgment standard + * `typeof window !== 'undefined' && typeof document !== 'undefined'`. + * This leads to a problem when axios post `FormData` in webWorker + */ + const isStandardBrowserWebWorkerEnv = (() => { + return ( + typeof WorkerGlobalScope !== 'undefined' && + self instanceof WorkerGlobalScope && + typeof self.importScripts === 'function' + ); +})(); + + export default { isBrowser: true, classes: { @@ -39,5 +57,6 @@ export default { Blob }, isStandardBrowserEnv, + isStandardBrowserWebWorkerEnv, protocols: ['http', 'https', 'file', 'blob', 'url', 'data'] };