@@ -8,6 +8,12 @@ var JSON_ENDS = {
8
8
'{' : / } $ /
9
9
} ;
10
10
var JSON_PROTECTION_PREFIX = / ^ \) \] \} ' , ? \n / ;
11
+ 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
+ } ;
11
17
12
18
function serializeValue ( v ) {
13
19
if ( isObject ( v ) ) {
@@ -330,6 +336,30 @@ function $HttpProvider() {
330
336
return useApplyAsync ;
331
337
} ;
332
338
339
+ var useLegacyPromse = true ;
340
+ /**
341
+ * @ngdoc method
342
+ * @name $httpProvider#useLegacyPromiseExtensions
343
+ * @description
344
+ *
345
+ * Configure `$http` service to return promises without the shorthand methods `success` and `error`.
346
+ * This should be used to make sure that applications work without these methods.
347
+ *
348
+ * Defaults to false. If no value is specified, returns the current configured value.
349
+ *
350
+ * @param {boolean= } value If true, `$http` will return a normal promise without the `success` and `error` methods.
351
+ *
352
+ * @returns {boolean|Object } If a value is specified, returns the $httpProvider for chaining.
353
+ * otherwise, returns the current configured value.
354
+ **/
355
+ this . useLegacyPromiseExtensions = function ( value ) {
356
+ if ( isDefined ( value ) ) {
357
+ useLegacyPromse = ! ! value ;
358
+ return this ;
359
+ }
360
+ return useLegacyPromse ;
361
+ } ;
362
+
333
363
/**
334
364
* @ngdoc property
335
365
* @name $httpProvider#interceptors
@@ -396,17 +426,15 @@ function $HttpProvider() {
396
426
*
397
427
* ## General usage
398
428
* The `$http` service is a function which takes a single argument — a configuration object —
399
- * that is used to generate an HTTP request and returns a {@link ng.$q promise}
400
- * with two $http specific methods: `success` and `error`.
429
+ * that is used to generate an HTTP request and returns a {@link ng.$q promise}.
401
430
*
402
431
* ```js
403
432
* // Simple GET request example :
404
433
* $http.get('/someUrl').
405
- * success (function(data, status, headers, config ) {
434
+ * then (function(response ) {
406
435
* // this callback will be called asynchronously
407
436
* // when the response is available
408
- * }).
409
- * error(function(data, status, headers, config) {
437
+ * }, function(response) {
410
438
* // called asynchronously if an error occurs
411
439
* // or server returns response with an error status.
412
440
* });
@@ -415,21 +443,23 @@ function $HttpProvider() {
415
443
* ```js
416
444
* // Simple POST request example (passing data) :
417
445
* $http.post('/someUrl', {msg:'hello word!'}).
418
- * success (function(data, status, headers, config ) {
446
+ * then (function(response ) {
419
447
* // this callback will be called asynchronously
420
448
* // when the response is available
421
- * }).
422
- * error(function(data, status, headers, config) {
449
+ * }, function(response) {
423
450
* // called asynchronously if an error occurs
424
451
* // or server returns response with an error status.
425
452
* });
426
453
* ```
427
454
*
455
+ * The response object has these properties:
428
456
*
429
- * Since the returned value of calling the $http function is a `promise`, you can also use
430
- * the `then` method to register callbacks, and these callbacks will receive a single argument –
431
- * an object representing the response. See the API signature and type info below for more
432
- * details.
457
+ * - **data** – `{string|Object}` – The response body transformed with the transform
458
+ * functions.
459
+ * - **status** – `{number}` – HTTP status code of the response.
460
+ * - **headers** – `{function([headerName])}` – Header getter function.
461
+ * - **config** – `{Object}` – The configuration object that was used to generate the request.
462
+ * - **statusText** – `{string}` – HTTP status text of the response.
433
463
*
434
464
* A response status code between 200 and 299 is considered a success status and
435
465
* will result in the success callback being called. Note that if the response is a redirect,
@@ -453,8 +483,8 @@ function $HttpProvider() {
453
483
* request data must be passed in for POST/PUT requests.
454
484
*
455
485
* ```js
456
- * $http.get('/someUrl').success (successCallback);
457
- * $http.post('/someUrl', data).success (successCallback);
486
+ * $http.get('/someUrl').then (successCallback);
487
+ * $http.post('/someUrl', data).then (successCallback);
458
488
* ```
459
489
*
460
490
* Complete list of shortcut methods:
@@ -468,6 +498,14 @@ function $HttpProvider() {
468
498
* - {@link ng.$http#patch $http.patch}
469
499
*
470
500
*
501
+ * ## Deprecation Notice
502
+ * <div class="alert alert-danger">
503
+ * The `$http` legacy promise methods `success` and `error` have been deprecated.
504
+ * Use the standard `then` method instead.
505
+ * If {@link $httpProvider#useLegacyPromiseExtensions `$httpProvider.useLegacyPromiseExtensions`} is set to
506
+ * `false` then these methods will throw {@link $http:legacy `$http/legacy`} error.
507
+ * </div>
508
+ *
471
509
* ## Setting HTTP Headers
472
510
*
473
511
* The $http service will automatically add certain HTTP headers to all requests. These defaults
@@ -511,7 +549,7 @@ function $HttpProvider() {
511
549
* data: { test: 'test' }
512
550
* }
513
551
*
514
- * $http(req).success (function(){...}).error( function(){...});
552
+ * $http(req).then (function(){...}, function(){...});
515
553
* ```
516
554
*
517
555
* ## Transforming Requests and Responses
@@ -743,7 +781,6 @@ function $HttpProvider() {
743
781
* In order to prevent collisions in environments where multiple Angular apps share the
744
782
* same domain or subdomain, we recommend that each application uses unique cookie name.
745
783
*
746
- *
747
784
* @param {object } config Object describing the request to be made and how it should be
748
785
* processed. The object has following properties:
749
786
*
@@ -788,20 +825,9 @@ function $HttpProvider() {
788
825
* - **responseType** - `{string}` - see
789
826
* [XMLHttpRequest.responseType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype).
790
827
*
791
- * @returns {HttpPromise } Returns a {@link ng.$q promise} object with the
792
- * standard `then` method and two http specific methods: `success` and `error`. The `then`
793
- * method takes two arguments a success and an error callback which will be called with a
794
- * response object. The `success` and `error` methods take a single argument - a function that
795
- * will be called when the request succeeds or fails respectively. The arguments passed into
796
- * these functions are destructured representation of the response object passed into the
797
- * `then` method. The response object has these properties:
828
+ * @returns {HttpPromise } Returns a {@link ng.$q `Promise}` that will be resolved to a response object
829
+ * when the request succeeds or fails.
798
830
*
799
- * - **data** – `{string|Object}` – The response body transformed with the transform
800
- * functions.
801
- * - **status** – `{number}` – HTTP status code of the response.
802
- * - **headers** – `{function([headerName])}` – Header getter function.
803
- * - **config** – `{Object}` – The configuration object that was used to generate the request.
804
- * - **statusText** – `{string}` – HTTP status text of the response.
805
831
*
806
832
* @property {Array.<Object> } pendingRequests Array of config objects for currently pending
807
833
* requests. This is primarily meant to be used for debugging purposes.
@@ -843,13 +869,12 @@ function $HttpProvider() {
843
869
$scope.response = null;
844
870
845
871
$http({method: $scope.method, url: $scope.url, cache: $templateCache}).
846
- success(function(data, status) {
847
- $scope.status = status;
848
- $scope.data = data;
849
- }).
850
- error(function(data, status) {
851
- $scope.data = data || "Request failed";
852
- $scope.status = status;
872
+ then(function(response) {
873
+ $scope.status = response.status;
874
+ $scope.data = response.data;
875
+ }, function(response) {
876
+ $scope.data = response.data || "Request failed";
877
+ $scope.status = response.status;
853
878
});
854
879
};
855
880
@@ -954,23 +979,28 @@ function $HttpProvider() {
954
979
promise = promise . then ( thenFn , rejectFn ) ;
955
980
}
956
981
957
- promise . success = function ( fn ) {
958
- assertArgFn ( fn , 'fn' ) ;
982
+ if ( useLegacyPromse ) {
983
+ promise . success = function ( fn ) {
984
+ assertArgFn ( fn , 'fn' ) ;
959
985
960
- promise . then ( function ( response ) {
961
- fn ( response . data , response . status , response . headers , config ) ;
962
- } ) ;
963
- return promise ;
964
- } ;
986
+ promise . then ( function ( response ) {
987
+ fn ( response . data , response . status , response . headers , config ) ;
988
+ } ) ;
989
+ return promise ;
990
+ } ;
965
991
966
- promise . error = function ( fn ) {
967
- assertArgFn ( fn , 'fn' ) ;
992
+ promise . error = function ( fn ) {
993
+ assertArgFn ( fn , 'fn' ) ;
968
994
969
- promise . then ( null , function ( response ) {
970
- fn ( response . data , response . status , response . headers , config ) ;
971
- } ) ;
972
- return promise ;
973
- } ;
995
+ promise . then ( null , function ( response ) {
996
+ fn ( response . data , response . status , response . headers , config ) ;
997
+ } ) ;
998
+ return promise ;
999
+ } ;
1000
+ } else {
1001
+ promise . success = $httpMinErrLegacyFn ( 'success' ) ;
1002
+ promise . error = $httpMinErrLegacyFn ( 'error' ) ;
1003
+ }
974
1004
975
1005
return promise ;
976
1006
0 commit comments