@@ -2,6 +2,7 @@ var Global = require("../../js/global/global");
2
2
var assign = require ( "../../js/assign/assign" ) ;
3
3
var namespace = require ( "can-namespace" ) ;
4
4
var parseURI = require ( '../../js/parse-uri/parse-uri' ) ;
5
+ var param = require ( "../../js/param/param" ) ;
5
6
6
7
/**
7
8
@module {function} can-util/dom/ajax/ajax ajax
@@ -38,8 +39,7 @@ var xhrs = [
38
39
// used to check for Cross Domain requests
39
40
var originUrl = parseURI ( Global ( ) . location . href ) ;
40
41
41
- var $ = { } ;
42
- $ . xhr = function ( ) {
42
+ var makeXhr = function ( ) {
43
43
if ( _xhrf != null ) {
44
44
return _xhrf ( ) ;
45
45
}
@@ -56,7 +56,8 @@ $.xhr = function () {
56
56
}
57
57
return function ( ) { } ;
58
58
} ;
59
- $ . _xhrResp = function ( xhr , options ) {
59
+
60
+ var _xhrResp = function ( xhr , options ) {
60
61
switch ( options . dataType || xhr . getResponseHeader ( "Content-Type" ) . split ( ";" ) [ 0 ] ) {
61
62
case "text/xml" :
62
63
case "xml" :
@@ -72,17 +73,9 @@ $._xhrResp = function (xhr, options) {
72
73
return xhr . responseText ;
73
74
}
74
75
} ;
75
- $ . _formData = function ( o ) {
76
- var kvps = [ ] , regEx = / % 2 0 / 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
+
84
77
module . exports = namespace . ajax = function ( o ) {
85
- var xhr = $ . xhr ( ) , timer , n = 0 ;
78
+ var xhr = makeXhr ( ) , timer , n = 0 ;
86
79
var deferred = { } ;
87
80
var promise = new Promise ( function ( resolve , reject ) {
88
81
deferred . resolve = resolve ;
@@ -129,7 +122,7 @@ module.exports = namespace.ajax = function (o) {
129
122
}
130
123
if ( xhr . status < 300 ) {
131
124
if ( o . success ) {
132
- o . success ( $ . _xhrResp ( xhr , o ) ) ;
125
+ o . success ( _xhrResp ( xhr , o ) ) ;
133
126
}
134
127
}
135
128
else if ( o . error ) {
@@ -140,7 +133,7 @@ module.exports = namespace.ajax = function (o) {
140
133
}
141
134
142
135
if ( xhr . status >= 200 && xhr . status < 300 ) {
143
- deferred . resolve ( $ . _xhrResp ( xhr , o ) ) ;
136
+ deferred . resolve ( _xhrResp ( xhr , o ) ) ;
144
137
} else {
145
138
deferred . reject ( xhr ) ;
146
139
}
@@ -155,7 +148,7 @@ module.exports = namespace.ajax = function (o) {
155
148
var url = o . url , data = null , type = o . type . toUpperCase ( ) ;
156
149
var isPost = type === "POST" || type === "PUT" ;
157
150
if ( ! isPost && o . data ) {
158
- url += "?" + $ . _formData ( o . data ) ;
151
+ url += "?" + param ( o . data ) ;
159
152
}
160
153
xhr . open ( type , url ) ;
161
154
@@ -167,15 +160,15 @@ module.exports = namespace.ajax = function (o) {
167
160
var isJson = o . dataType . indexOf ( "json" ) >= 0 ;
168
161
data = ( isJson && ! isSimpleCors ) ?
169
162
( typeof o . data === "object" ? JSON . stringify ( o . data ) : o . data ) :
170
- $ . _formData ( o . data ) ;
163
+ param ( o . data ) ;
171
164
172
165
// CORS simple: `Content-Type` has to be `application/x-www-form-urlencoded`:
173
166
xhr . setRequestHeader ( "Content-Type" , ( isJson && ! isSimpleCors ) ? "application/json" : "application/x-www-form-urlencoded" ) ;
174
167
}
175
168
176
169
// CORS simple: no custom headers, so we don't add `X-Requested-With` header:
177
170
if ( ! isSimpleCors ) {
178
- xhr . setRequestHeader ( "X-Requested-With" , "XMLHttpRequest" ) ;
171
+ xhr . setRequestHeader ( "X-Requested-With" , "XMLHttpRequest" ) ;
179
172
}
180
173
181
174
xhr . send ( data ) ;
0 commit comments