]> source.dussan.org Git - jquery.git/commitdiff
Ajax: Overwrite s.contentType with content-type header value, if any
authorChristian Wenz <christian@wenz.org>
Mon, 6 Apr 2020 19:15:55 +0000 (21:15 +0200)
committerGitHub <noreply@github.com>
Mon, 6 Apr 2020 19:15:55 +0000 (21:15 +0200)
This fixes the issue of "%20" in POST data being replaced with "+"
even for requests with content-type different from
"application/x-www-form-urlencoded", e.g. for "application/json".

Fixes gh-4119
Closes gh-4650

Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
Co-authored-by: Michał Gołębiowski-Owczarek <m.goleb@gmail.com>
src/ajax.js
test/unit/ajax.js

index 44ec6e83b6603f9abd01e9b83c3c3bc0a6e27201..0563c924b5009ef155ab56473ff0f8fb66a4203f 100644 (file)
@@ -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;
index ed17677ba9ca872dd28f27eee1fa85b616684593..e13c2713d2a7111c3b41711acf22462be41d5c5b 100644 (file)
@@ -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",