From a7ed9a7b6364273b1b964fd2cf9691dec2cbec6b Mon Sep 17 00:00:00 2001 From: Michał Gołębiowski-Owczarek Date: Wed, 1 Feb 2023 13:48:35 +0100 Subject: Ajax: Support binary data (including FormData) Two changes have been applied: * prefilters are now applied before data is converted to a string; this allows prefilters to disable such a conversion * a prefilter for binary data is added; it disables data conversion for non-string non-plain-object `data`; for `FormData` bodies, it removes manually-set `Content-Type` header - this is required as browsers need to append their own boundary to the header Ref gh-4150 Closes gh-5197 --- src/ajax/binary.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/ajax/binary.js (limited to 'src/ajax') diff --git a/src/ajax/binary.js b/src/ajax/binary.js new file mode 100644 index 000000000..e96661da7 --- /dev/null +++ b/src/ajax/binary.js @@ -0,0 +1,17 @@ +import jQuery from "../core.js"; + +import "../ajax.js"; + +jQuery.ajaxPrefilter( function( s ) { + + // Binary data needs to be passed to XHR as-is without stringification. + if ( typeof s.data !== "string" && !jQuery.isPlainObject( s.data ) ) { + s.processData = false; + } + + // `Content-Type` for requests with `FormData` bodies needs to be set + // by the browser as it needs to append the `boundary` it generated. + if ( s.data instanceof window.FormData ) { + s.contentType = false; + } +} ); -- cgit v1.2.3