8000 Ajax: Overwrite s.contentType with content-type header value, if any · jquery/jquery@065143c · GitHub
[go: up one dir, main page]

Skip to content

Commit 065143c

Browse files
wenzgibson042mgol
committed
Ajax: Overwrite s.contentType with content-type header value, if any
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 (cherry picked from 7fb90a6) Co-authored-by: Richard Gibson <richard.gibson@gmail.com> Co-authored-by: Michał Gołębiowski-Owczarek <m.goleb@gmail.com>
1 parent 1a4f10d commit 065143c

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/ajax.js

+9
Original file line numberDiff line numberDiff line change
@@ -855,5 +855,14 @@ jQuery.each( [ "get", "post" ], function( _i, method ) {
855855
};
856856
} );
857857

858+
jQuery.ajaxPrefilter( function( s ) {
859+
var i;
860+
for ( i in s.headers ) {
861+
if ( i.toLowerCase() === "content-type" ) {
862+
s.contentType = s.headers[ i ] || "";
863+
}
864+
}
865+
} );
866+
858867
return jQuery;
859868
} );

test/unit/ajax.js

+46
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,52 @@ QUnit.module( "ajax", {
13261326
};
13271327
} );
13281328

1329+
ajaxTest( "jQuery.ajax() - don't escape %20 with contentType override (gh-4119)", 1, function( assert ) {
1330+
return {
1331+
url: "bogus.html",
1332+
contentType: "application/x-www-form-urlencoded",
1333+
headers: { "content-type": "application/json" },
1334+
method: "post",
1335+
dataType: "json",
1336+
data: "{\"val\":\"%20\"}",
1337+
beforeSend: function( _, s ) {
1338+
assert.strictEqual( s.data, "{\"val\":\"%20\"}", "data is not %20-encoded" );
1339+
return false;
1340+
},
1341+
error: true
1342+
};
1343+
} );
1344+
1345+
ajaxTest( "jQuery.ajax() - escape %20 with contentType override (gh-4119)", 1, function( assert ) {
1346+
return {
1347+
url: "bogus.html",
1348+
contentType: "application/json",
1349+
headers: { "content-type": "application/x-www-form-urlencoded" },
1350+
method: "post",
1351+
dataType: "json",
1352+
data: "{\"val\":\"%20\"}",
1353+
beforeSend: function( _, s ) {
1354+
assert.strictEqual( s.data, "{\"val\":\"+\"}", "data is %20-encoded" );
1355+
return false;
1356+
},
1357+
error: true
1358+
};
1359+
} );
1360+
1361+
ajaxTest( "jQuery.ajax() - override contentType with header (gh-4119)", 1, function( assert ) {
1362+
return {
1363+
url: "bogus.html",
1364+
contentType: "application/json",
1365+
headers: { "content-type": "application/x-www-form-urlencoded" },
1366+
beforeSend: function( _, s ) {
1367+
assert.strictEqual( s.contentType, "application/x-www-form-urlencoded",
1368+
"contentType is overwritten" );
1369+
return false;
1370+
},
1371+
error: true
1372+
};
1373+
} );
1374+
13291375
ajaxTest( "jQuery.ajax() - data - no processing POST", 1, function( assert ) {
13301376
return {
13311377
url: "bogus.html",

0 commit comments

Comments
 (0)
0