Skip to content

Commit

Permalink
fix($httpBackend): cancelled JSONP requests will not print error in t…
Browse files Browse the repository at this point in the history
…he 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
Closes angular#5616
  • Loading branch information
Artemy Tregubenko authored and jamesdaily committed Jan 27, 2014
1 parent befa2d1 commit 011e9c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
} else {
completeRequest(callback, status || -2);
}
delete callbacks[callbackId];
callbacks[callbackId] = angular.noop;
});
} else {

Expand Down
9 changes: 7 additions & 2 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});


Expand Down

0 comments on commit 011e9c8

Please sign in to comment.