diff --git a/src/ng/http.js b/src/ng/http.js index f76f1cff25b8..e51c1ff92c13 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -963,22 +963,38 @@ function $HttpProvider() { } } + function is(type, obj) { + var clas = Object.prototype.toString.call(obj).slice(8, -1); + return obj !== undefined && obj !== null && clas === type; + } + + function toQueryString(params, parts, pk) { + forEachSorted(params, function(value, key) { + if (value == null || value == undefined) return; + if (pk) key = pk+'['+key+']'; + if (is('Array',value)) { + forEach(value, function(v,i) { + if(is('Object', v)){ + key = key+'['+i+']'; + toQueryString(v, parts, key) + } else { + parts.push(encodeUriQuery(key) + '=' + + encodeUriQuery(v)); + } + }); + return; + } else if (is('Object',value)) { + return toQueryString(value, parts, key) + } + parts.push(encodeUriQuery(key) + '=' + + encodeUriQuery(value)); + }); + } function buildUrl(url, params) { if (!params) return url; var parts = []; - forEachSorted(params, function(value, key) { - if (value == null || value == undefined) return; - if (!isArray(value)) value = [value]; - - forEach(value, function(v) { - if (isObject(v)) { - v = toJson(v); - } - parts.push(encodeUriQuery(key) + '=' + - encodeUriQuery(v)); - }); - }); + toQueryString(params, parts); return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&'); } diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index ec1cb7f1cb2c..eb190ee5076a 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -432,7 +432,7 @@ describe('$http', function() { it('should jsonify objects in params map', inject(function($httpBackend, $http) { - $httpBackend.expect('GET', '/url?a=1&b=%7B%22c%22:3%7D').respond(''); + $httpBackend.expect('GET', '/url?a=1&b%5Bc%5D=3').respond(''); $http({url: '/url', params: {a:1, b:{c:3}}, method: 'GET'}); }));