From e3d759491ca96a6764dfffaa3a9eac9261dfc50b Mon Sep 17 00:00:00 2001 From: wenzheng Date: Fri, 2 Dec 2022 01:15:23 +0800 Subject: [PATCH] fix: exception to sending formdata in webworker (#5139) Co-authored-by: Jay --- 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 42778d5f25..021da2a690 100644 --- a/lib/adapters/xhr.js +++ b/lib/adapters/xhr.js @@ -61,7 +61,7 @@ export default isXHRAdapterSupported && function (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'] };