Skip to content

Commit 064bcc2

Browse files
Fix #177 (Null values aren't serialized correctly)
Fix by serializing null and undefined values to blank strings. This is what jQuery also does.
1 parent 1594ab1 commit 064bcc2

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

ajax-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,18 @@ QUnit.asyncTest("crossDomain is true for relative requests", function(){
171171
start();
172172
});
173173
});
174+
175+
if (__dirname !== '/') {
176+
QUnit.asyncTest("correctly serializes null and undefined values (#177)", function () {
177+
ajax({
178+
type: "get",
179+
url: __dirname + "/test-result.txt",
180+
data: {
181+
foo: null
182+
}
183+
}).then(function (resp) {
184+
QUnit.equal(resp.message, "VALUE");
185+
start();
186+
});
187+
});
188+
}

ajax.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ $._xhrResp = function (xhr, options) {
7373
}
7474
};
7575
$._formData = function (o) {
76-
var kvps = [], regEx = /%20/g;
76+
var kvps = [], regEx = /%20/g, val;
7777
for (var k in o) {
78-
kvps.push(encodeURIComponent(k).replace(regEx, "+") + "=" + encodeURIComponent(o[k].toString()).replace(regEx, "+"));
78+
val = o[k];
79+
val = typeof val === "undefined" || val === null ? "" : val;
80+
kvps.push(encodeURIComponent(k).replace(regEx, "+") + "=" + encodeURIComponent(val.toString()).replace(regEx, "+"));
7981
}
8082
return kvps.join('&');
8183
};

0 commit comments

Comments
 (0)