Skip to content

Commit 8be67bd

Browse files
committed
ajax: use existing js.param for data serialization; added a test for js.param
1 parent 3809061 commit 8be67bd

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

ajax.js

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Global = require("../../js/global/global");
22
var assign = require("../../js/assign/assign");
33
var namespace = require("can-namespace");
44
var parseURI = require('../../js/parse-uri/parse-uri');
5+
var param = require("../../js/param/param");
56

67
/**
78
@module {function} can-util/dom/ajax/ajax ajax
@@ -38,8 +39,7 @@ var xhrs = [
3839
// used to check for Cross Domain requests
3940
var originUrl = parseURI(Global().location.href);
4041

41-
var $ = {};
42-
$.xhr = function () {
42+
var makeXhr = function () {
4343
if (_xhrf != null) {
4444
return _xhrf();
4545
}
@@ -56,7 +56,8 @@ $.xhr = function () {
5656
}
5757
return function () { };
5858
};
59-
$._xhrResp = function (xhr, options) {
59+
60+
var _xhrResp = function (xhr, options) {
6061
switch (options.dataType || xhr.getResponseHeader("Content-Type").split(";")[0]) {
6162
case "text/xml":
6263
case "xml":
@@ -72,17 +73,9 @@ $._xhrResp = function (xhr, options) {
7273
return xhr.responseText;
7374
}
7475
};
75-
$._formData = function (o) {
76-
var kvps = [], regEx = /%20/g, val;
77-
for (var k in o) {
78-
val = o[k];
79-
val = (val == null) ? "" : val;
80-
kvps.push(encodeURIComponent(k).replace(regEx, "+") + "=" + encodeURIComponent(val.toString()).replace(regEx, "+"));
81-
}
82-
return kvps.join('&');
83-
};
76+
8477
module.exports = namespace.ajax = function (o) {
85-
var xhr = $.xhr(), timer, n = 0;
78+
var xhr = makeXhr(), timer, n = 0;
8679
var deferred = {};
8780
var promise = new Promise(function(resolve,reject){
8881
deferred.resolve = resolve;
@@ -129,7 +122,7 @@ module.exports = namespace.ajax = function (o) {
129122
}
130123
if (xhr.status < 300) {
131124
if (o.success) {
132-
o.success($._xhrResp(xhr, o));
125+
o.success( _xhrResp(xhr, o) );
133126
}
134127
}
135128
else if (o.error) {
@@ -140,7 +133,7 @@ module.exports = namespace.ajax = function (o) {
140133
}
141134

142135
if (xhr.status >= 200 && xhr.status < 300) {
143-
deferred.resolve( $._xhrResp(xhr, o) );
136+
deferred.resolve( _xhrResp(xhr, o) );
144137
} else {
145138
deferred.reject( xhr );
146139
}
@@ -155,7 +148,7 @@ module.exports = namespace.ajax = function (o) {
155148
var url = o.url, data = null, type = o.type.toUpperCase();
156149
var isPost = type === "POST" || type === "PUT";
157150
if (!isPost && o.data) {
158-
url += "?" + $._formData(o.data);
151+
url += "?" + param(o.data);
159152
}
160153
xhr.open(type, url);
161154

@@ -167,15 +160,15 @@ module.exports = namespace.ajax = function (o) {
167160
var isJson = o.dataType.indexOf("json") >= 0;
168161
data = (isJson && !isSimpleCors) ?
169162
(typeof o.data === "object" ? JSON.stringify(o.data) : o.data):
170-
$._formData(o.data);
163+
param(o.data);
171164

172165
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
173166
xhr.setRequestHeader("Content-Type", (isJson && !isSimpleCors) ? "application/json" : "application/x-www-form-urlencoded");
174167
}
175168

176169
// CORS simple: no custom headers, so we don't add `X-Requested-With` header:
177170
if (!isSimpleCors){
178-
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest" );
171+
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
179172
}
180173

181174
xhr.send(data);

0 commit comments

Comments
 (0)