Skip to content

Commit 094a55b

Browse files
Merge pull request #31 from canjs/fix-xhr-formdata
Let xhr handles FormData object
2 parents 098f926 + 6e6d940 commit 094a55b

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

can-ajax-test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,50 @@ if (hasLocalServer) {
467467
});
468468
});
469469
}
470+
471+
472+
QUnit.test("It doesn't stringify FormData", function(assert) {
473+
var done = assert.async();
474+
var headers = {};
475+
var formData = new FormData();
476+
formData.append('foo', 'bar');
477+
478+
var restore = makeFixture(function () {
479+
var o = {};
480+
this.open = function (type, url) {
481+
o.url = url;
482+
};
483+
484+
this.send = function (data) {
485+
o.data = data;
486+
this.readyState = 4;
487+
this.status = 200;
488+
this.responseText = JSON.stringify(o);
489+
this.onreadystatechange();
490+
};
491+
492+
this.setRequestHeader = function (header, value) {
493+
if (header === "Content-Type") {
494+
o[header] = value;
495+
}
496+
};
497+
});
498+
499+
var origin = parseURI(GLOBAL().location.href);
500+
var url = origin.protocol + origin.authority + "/foo";
501+
502+
ajax({
503+
type: "post",
504+
url: url,
505+
data: formData
506+
}).then(function(value){
507+
assert.equal(value["Content-Type"], "application/json");
508+
assert.equal(value.url, url);
509+
}, function (reason) {
510+
assert.notOk(reason, "request failed with reason = ", reason);
511+
})
512+
.then(function(){
513+
restore();
514+
done();
515+
});
516+
});

can-ajax.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ var _xhrResp = function (xhr, options) {
119119

120120
function ajax(o) {
121121
var xhr = makeXhr(), timer, n = 0;
122-
var deferred = {};
122+
var deferred = {}, isFormData;
123123
var promise = new Promise(function(resolve,reject){
124124
deferred.resolve = resolve;
125125
deferred.reject = reject;
@@ -208,13 +208,18 @@ function ajax(o) {
208208
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
209209

210210
var isSimpleCors = o.crossDomain && ['GET', 'POST', 'HEAD'].indexOf(type) !== -1;
211+
isFormData = o.data instanceof FormData;
211212

212213
if (isPost) {
213-
data = (isJsonContentType && !isSimpleCors) ?
214-
(typeof o.data === "object" ? JSON.stringify(o.data) : o.data):
215-
param(o.data);
216-
217-
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
214+
if (isFormData) {
215+
// don't stringify FormData XHR handles it natively
216+
data = o.data;
217+
} else {
218+
data = (isJsonContentType && !isSimpleCors) ?
219+
(typeof o.data === "object" ? JSON.stringify(o.data) : o.data):
220+
param(o.data);
221+
}
222+
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
218223
var setContentType = (isJsonContentType && !isSimpleCors) ?
219224
"application/json" : "application/x-www-form-urlencoded";
220225
xhr.setRequestHeader("Content-Type", setContentType);

0 commit comments

Comments
 (0)