Skip to content

Commit 674348f

Browse files
committed
Do not set Content-Type for FormData payload
Closes #69
1 parent 4d4d754 commit 674348f

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

can-ajax-test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ if (hasLocalServer) {
542542

543543
QUnit.test("It doesn't stringify FormData", function(assert) {
544544
var done = assert.async();
545-
var headers = {};
545+
546546
var formData = new FormData();
547547
formData.append('foo', 'bar');
548548

@@ -575,8 +575,12 @@ QUnit.test("It doesn't stringify FormData", function(assert) {
575575
url: url,
576576
data: formData
577577
}).then(function(value){
578-
assert.equal(value["Content-Type"], "application/json");
579578
assert.equal(value.url, url);
579+
assert.equal(
580+
typeof value["Content-Type"],
581+
"undefined",
582+
"Content-Type should not be set"
583+
);
580584
}, function (reason) {
581585
assert.notOk(reason, "request failed with reason = ", reason);
582586
})

can-ajax.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,19 @@ function ajax(o) {
232232

233233
if (isPost) {
234234
if (isFormData) {
235-
// don't stringify FormData XHR handles it natively
235+
// do not set "Content-Type" let the browser handle it
236+
// do not stringify FormData XHR handles it natively
236237
data = o.data;
237238
} else {
238-
data = (isJsonContentType && !isSimpleCors) ?
239-
(typeof o.data === "object" ? JSON.stringify(o.data) : o.data):
240-
param(o.data);
239+
if (isJsonContentType && !isSimpleCors) {
240+
data = typeof o.data === "object" ? JSON.stringify(o.data) : o.data;
241+
xhr.setRequestHeader("Content-Type", "application/json");
242+
} else {
243+
data = param(o.data);
244+
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
245+
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
246+
}
241247
}
242-
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
243-
var setContentType = (isJsonContentType && !isSimpleCors) ?
244-
"application/json" : "application/x-www-form-urlencoded";
245-
xhr.setRequestHeader("Content-Type", setContentType);
246248
} else {
247249
xhr.setRequestHeader("Content-Type", o.contentType);
248250
}

0 commit comments

Comments
 (0)