aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/ajax.js9
-rw-r--r--test/unit/ajax.js46
2 files changed, 55 insertions, 0 deletions
diff --git a/src/ajax.js b/src/ajax.js
index 44ec6e83b..0563c924b 100644
--- a/src/ajax.js
+++ b/src/ajax.js
@@ -861,4 +861,13 @@ jQuery.each( [ "get", "post" ], function( _i, method ) {
};
} );
+jQuery.ajaxPrefilter( function( s ) {
+ var i;
+ for ( i in s.headers ) {
+ if ( i.toLowerCase() === "content-type" ) {
+ s.contentType = s.headers[ i ] || "";
+ }
+ }
+} );
+
export default jQuery;
diff --git a/test/unit/ajax.js b/test/unit/ajax.js
index ed17677ba..e13c2713d 100644
--- a/test/unit/ajax.js
+++ b/test/unit/ajax.js
@@ -1410,6 +1410,52 @@ QUnit.module( "ajax", {
};
} );
+ ajaxTest( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)", 1, function( assert ) {
+ return {
+ url: "bogus.html",
+ contentType: "application/x-www-form-urlencoded",
+ headers: { "content-type": "application/json" },
+ method: "post",
+ dataType: "json",
+ data: "{\"val\":\"%20\"}",
+ beforeSend: function( _, s ) {
+ assert.strictEqual( s.data, "{\"val\":\"%20\"}", "data is not %20-encoded" );
+ return false;
+ },
+ error: true
+ };
+ } );
+
+ ajaxTest( "jQuery.ajax() - escape %20 with contentType override (gh-4119)", 1, function( assert ) {
+ return {
+ url: "bogus.html",
+ contentType: "application/json",
+ headers: { "content-type": "application/x-www-form-urlencoded" },
+ method: "post",
+ dataType: "json",
+ data: "{\"val\":\"%20\"}",
+ beforeSend: function( _, s ) {
+ assert.strictEqual( s.data, "{\"val\":\"+\"}", "data is %20-encoded" );
+ return false;
+ },
+ error: true
+ };
+ } );
+
+ ajaxTest( "jQuery.ajax() - override contentType with header (gh-4119)", 1, function( assert ) {
+ return {
+ url: "bogus.html",
+ contentType: "application/json",
+ headers: { "content-type": "application/x-www-form-urlencoded" },
+ beforeSend: function( _, s ) {
+ assert.strictEqual( s.contentType, "application/x-www-form-urlencoded",
+ "contentType is overwritten" );
+ return false;
+ },
+ error: true
+ };
+ } );
+
ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) {
return {
url: "bogus.html",