From 039996ad8d4d87fbc8fa60e1d227fa95dafc12e6 Mon Sep 17 00:00:00 2001 From: Tom Davis Date: Fri, 14 Sep 2012 17:42:39 -0400 Subject: [PATCH] $http config.param expands array values properly. Today, calling e.g. $http(url, { params: { a: [1,2,3] } }) results in a query string like "?a=%5B1%2C2%2C3%5D" which is undesirable. This commit enhances buildURL to createa query string like "?a=1&a=2&a=3". --- src/ng/http.js | 13 +++++++++---- test/ng/httpSpec.js | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index aa0eca740642..9b133476b6fc 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -758,10 +758,15 @@ function $HttpProvider() { var parts = []; forEachSorted(params, function(value, key) { if (value == null || value == undefined) return; - if (isObject(value)) { - value = toJson(value); - } - parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); + if (!isArray(value)) value = [value]; + + forEach(value, function(v) { + if (isObject(v)) { + v = toJson(v); + } + parts.push(encodeURIComponent(key) + '=' + + encodeURIComponent(v)); + }); }); return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&'); } diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 45d31eef914b..060cadc97577 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -147,6 +147,12 @@ describe('$http', function() { $httpBackend.expect('GET', '/url?a=1&b=%7B%22c%22%3A3%7D').respond(''); $http({url: '/url', params: {a:1, b:{c:3}}, method: 'GET'}); })); + + + it('should expand arrays in params map', inject(function($httpBackend, $http) { + $httpBackend.expect('GET', '/url?a=1&a=2&a=3').respond(''); + $http({url: '/url', params: {a: [1,2,3]}, method: 'GET'}); + })); });