From d92e81d39c1660c79b370940b29ccf7ddfabe477 Mon Sep 17 00:00:00 2001 From: Artemy Tregubenko Date: Fri, 3 Jan 2014 16:22:55 +0100 Subject: [PATCH] fix($httpBackend): cancelled JSONP requests will not print error in the console When you cancel a JSONP request, angular deletes the callback for it. However the script still executes, and since the callback is now deleted and undefined, the script throws an exception visible in the console. The quick fix for this is not to delete the callback, but replace it with `angular.noop`. Closes #5615 Closes #5616 --- src/ng/httpBackend.js | 2 +- test/ng/httpBackendSpec.js | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 29f390e0402b..565e1b126fd4 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -54,7 +54,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc } else { completeRequest(callback, status || -2); } - delete callbacks[callbackId]; + callbacks[callbackId] = angular.noop; }); } else { diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 1be31984d2f3..49514d8e3c75 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -330,7 +330,7 @@ describe('$httpBackend', function() { script.onload(); } - expect(callbacks[callbackId]).toBeUndefined(); + expect(callbacks[callbackId]).toBe(angular.noop); expect(fakeDocument.body.removeChild).toHaveBeenCalledOnceWith(script); }); @@ -397,7 +397,7 @@ describe('$httpBackend', function() { }); - it('should abort request on timeout', function() { + it('should abort request on timeout and replace callback with noop', function() { callback.andCallFake(function(status, response) { expect(status).toBe(-1); }); @@ -406,9 +406,14 @@ describe('$httpBackend', function() { expect(fakeDocument.$$scripts.length).toBe(1); expect(fakeTimeout.delays[0]).toBe(2000); + var script = fakeDocument.$$scripts.shift(), + callbackId = script.src.match(SCRIPT_URL)[2]; + fakeTimeout.flush(); expect(fakeDocument.$$scripts.length).toBe(0); expect(callback).toHaveBeenCalledOnce(); + + expect(callbacks[callbackId]).toBe(angular.noop); });