From 3366b35a6fc9380e59e988600c7efb023fb15d01 Mon Sep 17 00:00:00 2001 From: Dan Barua Date: Fri, 11 Jul 2014 17:34:52 +0100 Subject: [PATCH 1/2] fix($http): fix double-quoted date issue when encoding params $http was wrapping dates in double quotes leading to query strings like this: ?date=%222014-07-07T23:00:00.000Z%22 Instead of calling JSON.stringify, this fix checks to see if a param object has a toJSON() function and uses that for encoding, falling back to JSON.stringify as per the previous behaviour. Closes #8150 and #6128 --- src/ng/http.js | 9 +++++++-- test/ng/httpSpec.js | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index f009acd8de83..d02bdf256b86 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -1,4 +1,4 @@ -'use strict'; +'use strict'; /** * Parse headers into key value object @@ -990,7 +990,12 @@ function $HttpProvider() { forEach(value, function(v) { if (isObject(v)) { - v = toJson(v); + if (isDefined(v.toJSON)){ + v = v.toJSON(); + } + else { + v = toJson(v); + } } parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v)); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index 410bb502a779..9547c32d1763 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -341,6 +341,11 @@ describe('$http', function() { $httpBackend.expect('GET', '/url').respond(''); $http({url: '/url', params: {}, method: 'GET'}); }); + + it('should not double quote dates', function() { + $httpBackend.expect('GET', '/url?date=2014-07-15T17:30:00.000Z').respond(''); + $http({url: '/url', params: {date:new Date('2014-07-15T17:30:00.000Z')}, method: 'GET'}); + }); }); From 17931a5b7fbc0319207ab37a7d12b75653553209 Mon Sep 17 00:00:00 2001 From: Dan Barua Date: Tue, 15 Jul 2014 09:06:19 +0100 Subject: [PATCH 2/2] fix($http): fix double-quoted date issue when encoding params Special case date handling rather than calling toJSON as we always need a string representation of the object. --- src/ng/http.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index d02bdf256b86..6c0557385640 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -990,12 +990,12 @@ function $HttpProvider() { forEach(value, function(v) { if (isObject(v)) { - if (isDefined(v.toJSON)){ - v = v.toJSON(); - } - else { - v = toJson(v); - } + if (v instanceof Date){ + v = v.toISOString(); //toISOString() only supported in IE8 and above + } + else if (isObject(v)) { + v = toJson(v); + } } parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));