Skip to content

Commit 07d43f3

Browse files
committed
Ajax: send correct "Content-type" if data is set in "beforeSend" method
1 parent 4e7f34f commit 07d43f3

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/ajax.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,6 @@ jQuery.extend({
579579
}
580580
}
581581

582-
// Set the correct header, if data is being sent
583-
if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
584-
jqXHR.setRequestHeader( "Content-Type", s.contentType );
585-
}
586-
587582
// Set the Accepts header for the server, depending on the dataType
588583
jqXHR.setRequestHeader(
589584
"Accept",
@@ -606,6 +601,11 @@ jQuery.extend({
606601
return jqXHR.abort();
607602
}
608603

604+
// Set the correct header, if data is being sent
605+
if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) {
606+
jqXHR.setRequestHeader( "Content-Type", s.contentType );
607+
}
608+
609609
// Aborting is no longer a cancellation
610610
strAbort = "abort";
611611

test/unit/ajax.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,37 @@ module( "ajax", {
20222022
});
20232023
});
20242024

2025+
asyncTest( "Send correct \"Content-type\" if data is set in \"beforeSend\" method gh#1801", 2,
2026+
function() {
2027+
var oldSetRequestHeader;
2028+
2029+
jQuery.ajax({
2030+
url: "/",
2031+
method: "post",
2032+
beforeSend: function( xhr, settings ) {
2033+
oldSetRequestHeader = xhr.setRequestHeader;
2034+
settings.data = "test";
2035+
2036+
xhr.setRequestHeader = function( name, value ) {
2037+
strictEqual( name, "Content-Type", "correct header" );
2038+
strictEqual( value,
2039+
"application/x-www-form-urlencoded; charset=UTF-8",
2040+
"correct value"
2041+
);
2042+
};
2043+
}
2044+
}).done(function( data, textStatus, jqXHR ) {
2045+
jqXHR.old = oldSetRequestHeader;
2046+
2047+
start();
2048+
2049+
}).fail(function( jqXHR ) {
2050+
jqXHR.old = oldSetRequestHeader;
2051+
2052+
start();
2053+
});
2054+
});
2055+
20252056
//----------- jQuery.active
20262057

20272058
test( "jQuery.active", 1, function() {

0 commit comments

Comments
 (0)