Skip to content

Commit dde3cdc

Browse files
committed
Make sure that requests without a body don't set contentType, and a zero-length body is sent rather than null. Possible fix for #6811 and #6674, needs testing from someone who could repro the original problem.
1 parent 722af53 commit dde3cdc

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/ajax.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var jsc = jQuery.now(),
44
rscript = /<script(.|\s)*?\/script>/gi,
55
rselectTextarea = /select|textarea/i,
66
rinput = /color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week/i,
7+
rnoContent = /^GET|HEAD|DELETE$/,
78
jsre = /\=\?(&|$)/,
89
rquery = /\?/,
910
rts = /(\?|&)_=.*?(&|$)/,
@@ -202,7 +203,7 @@ jQuery.extend({
202203

203204
ajax: function( origSettings ) {
204205
var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings),
205-
jsonp, status, data, type = s.type.toUpperCase();
206+
jsonp, status, data, type = s.type.toUpperCase(), noContent = rnoContent.test(type);
206207

207208
// Use original (not extended) context object if it was provided
208209
s.context = ( origSettings && origSettings.context !== undefined )? origSettings.context : s;
@@ -351,8 +352,8 @@ jQuery.extend({
351352

352353
// Need an extra try/catch for cross domain requests in Firefox 3
353354
try {
354-
// Set the correct header, if data is being sent
355-
if ( s.data || type === "POST" || origSettings.contentType ) {
355+
// Set content-type if data specified and content-body is valid for this type
356+
if ( (s.data != null && !noContent) || (origSettings && origSettings.contentType) ) {
356357
xhr.setRequestHeader("Content-Type", s.contentType);
357358
}
358359

@@ -487,7 +488,7 @@ jQuery.extend({
487488

488489
// Send the data
489490
try {
490-
xhr.send( (type !== "GET" && s.data) || null );
491+
xhr.send( (noContent || s.data == null)? null : s.data );
491492

492493
} catch( e ) {
493494
jQuery.ajax.handleError( s, xhr, null, e );

0 commit comments

Comments
 (0)