diff options
| -rw-r--r-- | src/service/xhr.js | 7 | ||||
| -rw-r--r-- | test/service/xhrSpec.js | 39 |
2 files changed, 44 insertions, 2 deletions
diff --git a/src/service/xhr.js b/src/service/xhr.js index 2f003398..6158b84f 100644 --- a/src/service/xhr.js +++ b/src/service/xhr.js @@ -79,8 +79,11 @@ angularServiceInject('$xhr', function($browser, $error, $log){ } $browser.xhr(method, url, post, function(code, response){ try { - if (isString(response) && /^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) { - response = fromJson(response, true); + if (isString(response)) { + if (response.match(/^\)\]\}',\n/)) response=response.substr(6); + if (/^\s*[\[\{]/.exec(response) && /[\}\]]\s*$/.exec(response)) { + response = fromJson(response, true); + } } if (code == 200) { callback(code, response); diff --git a/test/service/xhrSpec.js b/test/service/xhrSpec.js index 35861a92..fc51eff2 100644 --- a/test/service/xhrSpec.js +++ b/test/service/xhrSpec.js @@ -44,4 +44,43 @@ describe('$xhr', function() { expect($log.error).wasCalledWith("MyException"); }); + + + it('should automatically deserialize json objects', function() { + var response; + + $browserXhr.expectGET('/foo').respond('{"foo":"bar","baz":23}'); + $xhr('GET', '/foo', function(code, resp) { + response = resp; + }); + $browserXhr.flush(); + + expect(response).toEqual({foo:'bar', baz:23}); + }); + + + it('should automatically deserialize json arrays', function() { + var response; + + $browserXhr.expectGET('/foo').respond('[1, "abc", {"foo":"bar"}]'); + $xhr('GET', '/foo', function(code, resp) { + response = resp; + }); + $browserXhr.flush(); + + expect(response).toEqual([1, 'abc', {foo:'bar'}]); + }); + + + it('should automatically deserialize json with security prefix', function() { + var response; + + $browserXhr.expectGET('/foo').respond(')]}\',\n[1, "abc", {"foo":"bar"}]'); + $xhr('GET', '/foo', function(code, resp) { + response = resp; + }); + $browserXhr.flush(); + + expect(response).toEqual([1, 'abc', {foo:'bar'}]); + }); }); |
