Skip to content

Commit 618d935

Browse files
author
Artemy Tregubenko
committed
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 angular#5615
1 parent 0559652 commit 618d935

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/ng/httpBackend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
5454
} else {
5555
completeRequest(callback, status || -2);
5656
}
57-
delete callbacks[callbackId];
57+
callbacks[callbackId] = angular.noop;
5858
});
5959
} else {
6060

test/ng/httpBackendSpec.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ describe('$httpBackend', function() {
328328
script.onload();
329329
}
330330

331-
expect(callbacks[callbackId]).toBeUndefined();
331+
expect(callbacks[callbackId]).toBe(angular.noop);
332332
expect(fakeDocument.body.removeChild).toHaveBeenCalledOnceWith(script);
333333
});
334334

@@ -395,7 +395,7 @@ describe('$httpBackend', function() {
395395
});
396396

397397

398-
it('should abort request on timeout', function() {
398+
it('should abort request on timeout and replace callback with noop', function() {
399399
callback.andCallFake(function(status, response) {
400400
expect(status).toBe(-1);
401401
});
@@ -404,9 +404,14 @@ describe('$httpBackend', function() {
404404
expect(fakeDocument.$$scripts.length).toBe(1);
405405
expect(fakeTimeout.delays[0]).toBe(2000);
406406

407+
var script = fakeDocument.$$scripts.shift(),
408+
callbackId = script.src.match(SCRIPT_URL)[2];
409+
407410
fakeTimeout.flush();
408411
expect(fakeDocument.$$scripts.length).toBe(0);
409412
expect(callback).toHaveBeenCalledOnce();
413+
414+
expect(callbacks[callbackId]).toBe(angular.noop);
410415
});
411416

412417

0 commit comments

Comments
 (0)