Skip to content

Commit f63d7f7

Browse files
Don’t set a default Content-Type on an Ajax.Request if the user specifies a falsy contentType option. (Closes prototypejs#313)
1 parent 484287d commit f63d7f7

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/prototype/ajax/request.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ Ajax.Request = Class.create(Ajax.Base, {
236236
};
237237

238238
if (this.method == 'post') {
239-
headers['Content-type'] = this.options.contentType +
240-
(this.options.encoding ? '; charset=' + this.options.encoding : '');
239+
// Don't set the `Content-Type` header if the user has explicitly set
240+
// `contentType` to anything falsy.
241+
if (this.options.contentType) {
242+
headers['Content-type'] = this.options.contentType +
243+
(this.options.encoding ? '; charset=' + this.options.encoding : '');
244+
}
241245

242246
/* Force "Connection: close" for older Mozilla browsers to work
243247
* around a bug where XMLHttpRequest sends an incorrect

test/unit/tests/ajax.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,17 @@ suite("Ajax", function () {
495495
Ajax.Request.prototype.isSameOrigin = isSameOrigin;
496496
});
497497

498+
test('can omit content-type', function () {
499+
new Ajax.Request('/inspect', extendDefault({
500+
method: 'post',
501+
contentType: false,
502+
onSuccess: function (response) {
503+
// If we omit Content-Type, the browser will provide its own.
504+
var contentType = response.responseJSON.headers['content-type'];
505+
assert(contentType.indexOf('text/plain') > -1);
506+
}
507+
}));
508+
});
509+
498510
});
499511

0 commit comments

Comments
 (0)