Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 065143c

Browse filesBrowse 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
Copy full SHA for 065143c

File tree

Expand file treeCollapse file tree

2 files changed

+55
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+55
-0
lines changed
Open diff view settings
Collapse file

‎src/ajax.js‎

Copy file name to clipboardExpand all lines: src/ajax.js
+9Lines changed: 9 additions & 0 deletions
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
} );
Collapse file

‎test/unit/ajax.js‎

Copy file name to clipboardExpand all lines: test/unit/ajax.js
+46Lines changed: 46 additions & 0 deletions
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.