Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b54a39e

Browse files
committed
feat($http): remove deprecated callback methods: success()/error()
Closes #15157 BREAKING CHANGE: `$http`'s deprecated custom callback methods - `success()` and `error()` - have been removed. You can use the standard `then()`/`catch()` promise methods instead, but note that the method signatures and return values are different. `success(fn)` can be replaced with `then(fn)`, and `error(fn)` can be replaced with either `then(null, fn)` or `catch(fn)`. Before: ```js $http(...). success(function onSuccess(data, status, headers, config) { // Handle success ... }). error(function onError(data, status, headers, config) { // Handle error ... }); ``` After: ```js $http(...). then(function onSuccess(response) { // Handle success var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }, function onError(response) { // Handle error var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }); // or $http(...). then(function onSuccess(response) { // Handle success var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }). catch(function onError(response) { // Handle error var data = response.data; var status = response.status; var statusText = response.statusText; var headers = response.headers; var config = response.config; ... }); ``` **Note:** There is a subtle difference between the variations showed above. When using `$http(...).success(onSuccess).error(onError)` or `$http(...).then(onSuccess, onError)`, the `onError()` callback will only handle errors/rejections produced by the `$http()` call. If the `onSuccess()` callback produces an error/rejection, it won't be handled by `onError()` and might go unnoticed. In contrast, when using `$http(...).then(onSuccess).catch(onError)`, `onError()` will handle errors/rejections produced by both `$http()` _and_ `onSuccess()`.
1 parent fb66341 commit b54a39e

File tree

4 files changed

+95
-331
lines changed

4 files changed

+95
-331
lines changed

docs/content/error/$http/legacy.ngdoc

-45
This file was deleted.

src/ng/http.js

-60
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ var JSON_ENDS = {
99
};
1010
var JSON_PROTECTION_PREFIX = /^\)\]\}',?\n/;
1111
var $httpMinErr = minErr('$http');
12-
var $httpMinErrLegacyFn = function(method) {
13-
return function() {
14-
throw $httpMinErr('legacy', 'The method `{0}` on the promise returned from `$http` has been disabled.', method);
15-
};
16-
};
1712

1813
function serializeValue(v) {
1914
if (isObject(v)) {
@@ -346,30 +341,6 @@ function $HttpProvider() {
346341
return useApplyAsync;
347342
};
348343

349-
var useLegacyPromise = true;
350-
/**
351-
* @ngdoc method
352-
* @name $httpProvider#useLegacyPromiseExtensions
353-
* @description
354-
*
355-
* Configure `$http` service to return promises without the shorthand methods `success` and `error`.
356-
* This should be used to make sure that applications work without these methods.
357-
*
358-
* Defaults to true. If no value is specified, returns the current configured value.
359-
*
360-
* @param {boolean=} value If true, `$http` will return a promise with the deprecated legacy `success` and `error` methods.
361-
*
362-
* @returns {boolean|Object} If a value is specified, returns the $httpProvider for chaining.
363-
* otherwise, returns the current configured value.
364-
**/
365-
this.useLegacyPromiseExtensions = function(value) {
366-
if (isDefined(value)) {
367-
useLegacyPromise = !!value;
368-
return this;
369-
}
370-
return useLegacyPromise;
371-
};
372-
373344
/**
374345
* @ngdoc property
375346
* @name $httpProvider#interceptors
@@ -503,14 +474,6 @@ function $HttpProvider() {
503474
* $httpBackend.flush();
504475
* ```
505476
*
506-
* ## Deprecation Notice
507-
* <div class="alert alert-danger">
508-
* The `$http` legacy promise methods `success` and `error` have been deprecated.
509-
* Use the standard `then` method instead.
510-
* If {@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`} is set to
511-
* `false` then these methods will throw {@link $http:legacy `$http/legacy`} error.
512-
* </div>
513-
*
514477
* ## Setting HTTP Headers
515478
*
516479
* The $http service will automatically add certain HTTP headers to all requests. These defaults
@@ -1000,29 +963,6 @@ function $HttpProvider() {
1000963
promise = chainInterceptors(promise, responseInterceptors);
1001964
promise = promise.finally(completeOutstandingRequest);
1002965

1003-
if (useLegacyPromise) {
1004-
promise.success = function(fn) {
1005-
assertArgFn(fn, 'fn');
1006-
1007-
promise.then(function(response) {
1008-
fn(response.data, response.status, response.headers, config);
1009-
});
1010-
return promise;
1011-
};
1012-
1013-
promise.error = function(fn) {
1014-
assertArgFn(fn, 'fn');
1015-
1016-
promise.then(null, function(response) {
1017-
fn(response.data, response.status, response.headers, config);
1018-
});
1019-
return promise;
1020-
};
1021-
} else {
1022-
promise.success = $httpMinErrLegacyFn('success');
1023-
promise.error = $httpMinErrLegacyFn('error');
1024-
}
1025-
1026966
return promise;
1027967

1028968

src/ng/sce.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,8 @@ function $SceDelegateProvider() {
612612
* .controller('AppController', ['$http', '$templateCache', '$sce',
613613
* function AppController($http, $templateCache, $sce) {
614614
* var self = this;
615-
* $http.get('test_data.json', {cache: $templateCache}).success(function(userComments) {
616-
* self.userComments = userComments;
615+
* $http.get('test_data.json', {cache: $templateCache}).then(function(response) {
616+
* self.userComments = response.data;
617617
* });
618618
* self.explicitlyTrustedHtml = $sce.trustAsHtml(
619619
* '<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses ' +

0 commit comments

Comments
 (0)