@@ -220,8 +220,8 @@ function $HttpProvider() {
220
220
**/
221
221
var interceptorFactories = this . interceptors = [ ] ;
222
222
223
- this . $get = [ '$httpBackend' , '$$cookieReader' , '$cacheFactory' , '$rootScope' , '$q' , '$injector' ,
224
- function ( $httpBackend , $$cookieReader , $cacheFactory , $rootScope , $q , $injector ) {
223
+ this . $get = [ '$httpBackend' , '$httpUrlParams' , '$ $cookieReader', '$cacheFactory' , '$rootScope' , '$q' , '$injector' ,
224
+ function ( $httpBackend , $httpUrlParams , $ $cookieReader, $cacheFactory , $rootScope , $q , $injector ) {
225
225
226
226
var defaultCache = $cacheFactory ( '$http' ) ;
227
227
@@ -1028,7 +1028,7 @@ function $HttpProvider() {
1028
1028
cache ,
1029
1029
cachedResp ,
1030
1030
reqHeaders = config . headers ,
1031
- url = buildUrl ( config . url , config . params ) ;
1031
+ url = buildUrl ( config . url , config . params , config . paramsMode ) ;
1032
1032
1033
1033
$http . pendingRequests . push ( config ) ;
1034
1034
promise . then ( removePendingReq , removePendingReq ) ;
@@ -1135,29 +1135,86 @@ function $HttpProvider() {
1135
1135
}
1136
1136
1137
1137
1138
- function buildUrl ( url , params ) {
1139
- if ( ! params ) return url ;
1140
- var parts = [ ] ;
1141
- forEachSorted ( params , function ( value , key ) {
1142
- if ( value === null || isUndefined ( value ) ) return ;
1143
- if ( ! isArray ( value ) ) value = [ value ] ;
1144
-
1145
- forEach ( value , function ( v ) {
1146
- if ( isObject ( v ) ) {
1147
- if ( isDate ( v ) ) {
1148
- v = v . toISOString ( ) ;
1149
- } else {
1150
- v = toJson ( v ) ;
1151
- }
1152
- }
1153
- parts . push ( encodeUriQuery ( key ) + '=' +
1154
- encodeUriQuery ( v ) ) ;
1155
- } ) ;
1156
- } ) ;
1157
- if ( parts . length > 0 ) {
1158
- url += ( ( url . indexOf ( '?' ) == - 1 ) ? '?' : '&' ) + parts . join ( '&' ) ;
1138
+ function buildUrl ( url , params , mode ) {
1139
+ params = $httpUrlParams . serialize ( params , mode ) ;
1140
+ if ( params ) {
1141
+ url += ( ( url . indexOf ( '?' ) == - 1 ) ? '?' : '&' ) + params ;
1159
1142
}
1160
1143
return url ;
1161
1144
}
1162
1145
} ] ;
1163
1146
}
1147
+
1148
+ /**
1149
+ * @ngdoc provider
1150
+ * @name $httpUrlParamsProvider
1151
+ * @description
1152
+ * Use `$httpUrlParamsProvider` to change the default behavior of the {@link ng.$httpUrlParams $httpUrlParams} service.
1153
+ * */
1154
+ function $HttpUrlParamsProvider ( ) {
1155
+
1156
+ var paramSerializers = createMap ( ) ;
1157
+
1158
+ function serializeParams ( params , addArrayMarker ) {
1159
+ var parts = [ ] ;
1160
+
1161
+ if ( ! params ) return '' ;
1162
+
1163
+ forEachSorted ( params , function ( value , key ) {
1164
+ if ( value === null || isUndefined ( value ) ) return ;
1165
+ if ( ! isArray ( value ) ) value = [ value ] ;
1166
+
1167
+ forEach ( value , function ( v ) {
1168
+ if ( isObject ( v ) ) {
1169
+ v = isDate ( v ) ? v . toISOString ( ) : toJson ( v ) ;
1170
+ }
1171
+ parts . push ( encodeUriQuery ( key ) + ( addArrayMarker ? '[]' : '' ) + '=' + encodeUriQuery ( v ) ) ;
1172
+ } ) ;
1173
+ } ) ;
1174
+
1175
+ return parts . join ( '&' ) ;
1176
+ }
1177
+
1178
+
1179
+ this . defaultMode = 'traditional' ;
1180
+
1181
+
1182
+ this . registerSerializer = function registerSerializer ( mode , serFn ) {
1183
+ paramSerializers [ mode ] = serFn ;
1184
+ } ;
1185
+
1186
+ /**
1187
+ * @ngdoc service
1188
+ * @name $httpUrlParams
1189
+ *
1190
+ * @description
1191
+ * The `$httpUrlParams` service is responsible for serializing request params
1192
+ * (expressed as a JavaScript object) to a string.
1193
+ * It abstracts the way in which request params are transformed, grouped, encoded etc.
1194
+ *
1195
+ * Normally you wouldn't use this service directly in the application's code but rather override this service
1196
+ * to enable custom serialization schemas for URL parameters.
1197
+ *
1198
+ * The `$httpUrlParams` service comes handy while unit testing with {@link ngMock.$httpBackend $httpBackend mock},
1199
+ * as one can inject `$httpUrlParams` into a test and serialize URL params as {@link ng.$http $http} service.
1200
+ */
1201
+ this . $get = function ( ) {
1202
+
1203
+ var provider = this ;
1204
+ var HttpUrlParams = { } ;
1205
+
1206
+ HttpUrlParams . serialize = function ( params , mode ) {
1207
+ return paramSerializers [ lowercase ( mode ) || provider . defaultMode ] ( params ) ;
1208
+ } ;
1209
+
1210
+ return HttpUrlParams ;
1211
+ } ;
1212
+
1213
+ this . registerSerializer ( this . defaultMode , function ( params ) {
1214
+ return serializeParams ( params , false ) ;
1215
+ } ) ;
1216
+
1217
+ this . registerSerializer ( 'jquery' , function ( params ) {
1218
+ return serializeParams ( params , false ) ;
1219
+ } ) ;
1220
+ }
0 commit comments