@@ -330,6 +330,29 @@ function $HttpProvider() {
330
330
return useApplyAsync ;
331
331
} ;
332
332
333
+ var useHttpPromise = true ;
334
+ /**
335
+ * @ngdoc method
336
+ * @name $httpProvider#useLegacyMethods
337
+ *
338
+ * Configure $http service to return promises without the shorthand methods `success` and `error`. It should
339
+ * be used to make sure that applications work without these methods.
340
+ *
341
+ * Defaults to false. If no value is specified, returns the current configured value.
342
+ *
343
+ * @param {boolean= } value If true, $http will return a promise without the `success` and `error methods.
344
+ *
345
+ * @returns {boolean|Object } If a value is specified, returns the $httpProvider for chaining.
346
+ * otherwise, returns the current configured value.
347
+ **/
348
+ this . useLegacyMethods = function ( value ) {
349
+ if ( isDefined ( value ) ) {
350
+ useHttpPromise = ! ! value ;
351
+ return this ;
352
+ }
353
+ return useHttpPromise ;
354
+ } ;
355
+
333
356
/**
334
357
* @ngdoc property
335
358
* @name $httpProvider#interceptors
@@ -396,17 +419,15 @@ function $HttpProvider() {
396
419
*
397
420
* ## General usage
398
421
* 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`.
422
+ * that is used to generate an HTTP request and returns a {@link ng.$q promise}.
401
423
*
402
424
* ```js
403
425
* // Simple GET request example :
404
426
* $http.get('/someUrl').
405
- * success (function(data, status, headers, config ) {
427
+ * then (function(response ) {
406
428
* // this callback will be called asynchronously
407
429
* // when the response is available
408
- * }).
409
- * error(function(data, status, headers, config) {
430
+ * }, function(response) {
410
431
* // called asynchronously if an error occurs
411
432
* // or server returns response with an error status.
412
433
* });
@@ -415,21 +436,23 @@ function $HttpProvider() {
415
436
* ```js
416
437
* // Simple POST request example (passing data) :
417
438
* $http.post('/someUrl', {msg:'hello word!'}).
418
- * success (function(data, status, headers, config ) {
439
+ * then (function(response ) {
419
440
* // this callback will be called asynchronously
420
441
* // when the response is available
421
- * }).
422
- * error(function(data, status, headers, config) {
442
+ * }, function(response) {
423
443
* // called asynchronously if an error occurs
424
444
* // or server returns response with an error status.
425
445
* });
426
446
* ```
427
447
*
448
+ * The response object has these properties:
428
449
*
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.
450
+ * - **data** – `{string|Object}` – The response body transformed with the transform
451
+ * functions.
452
+ * - **status** – `{number}` – HTTP status code of the response.
453
+ * - **headers** – `{function([headerName])}` – Header getter function.
454
+ * - **config** – `{Object}` – The configuration object that was used to generate the request.
455
+ * - **statusText** – `{string}` – HTTP status text of the response.
433
456
*
434
457
* A response status code between 200 and 299 is considered a success status and
435
458
* will result in the success callback being called. Note that if the response is a redirect,
@@ -453,8 +476,8 @@ function $HttpProvider() {
453
476
* request data must be passed in for POST/PUT requests.
454
477
*
455
478
* ```js
456
- * $http.get('/someUrl').success (successCallback);
457
- * $http.post('/someUrl', data).success (successCallback);
479
+ * $http.get('/someUrl').then (successCallback);
480
+ * $http.post('/someUrl', data).then (successCallback);
458
481
* ```
459
482
*
460
483
* Complete list of shortcut methods:
@@ -511,7 +534,7 @@ function $HttpProvider() {
511
534
* data: { test: 'test' }
512
535
* }
513
536
*
514
- * $http(req).success (function(){...}).error( function(){...});
537
+ * $http(req).then (function(){...}, function(){...});
515
538
* ```
516
539
*
517
540
* ## Transforming Requests and Responses
@@ -794,14 +817,12 @@ function $HttpProvider() {
794
817
* response object. The `success` and `error` methods take a single argument - a function that
795
818
* will be called when the request succeeds or fails respectively. The arguments passed into
796
819
* these functions are destructured representation of the response object passed into the
797
- * `then` method. The response object has these properties:
820
+ * `then` method.
798
821
*
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.
822
+ * <div class="alert alert-error">
823
+ * **Note:** the short hand methods `success` and `error` are deprecated.
824
+ * Use the standard `then` method instead.
825
+ * </div>
805
826
*
806
827
* @property {Array.<Object> } pendingRequests Array of config objects for currently pending
807
828
* requests. This is primarily meant to be used for debugging purposes.
@@ -843,13 +864,12 @@ function $HttpProvider() {
843
864
$scope.response = null;
844
865
845
866
$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;
867
+ then(function(response) {
868
+ $scope.status = response.status;
869
+ $scope.data = response.data;
870
+ }, function(response) {
871
+ $scope.data = response.data || "Request failed";
872
+ $scope.status = response.status;
853
873
});
854
874
};
855
875
@@ -954,23 +974,25 @@ function $HttpProvider() {
954
974
promise = promise . then ( thenFn , rejectFn ) ;
955
975
}
956
976
957
- promise . success = function ( fn ) {
958
- assertArgFn ( fn , 'fn' ) ;
977
+ if ( useHttpPromise ) {
978
+ promise . success = function ( fn ) {
979
+ assertArgFn ( fn , 'fn' ) ;
959
980
960
- promise . then ( function ( response ) {
961
- fn ( response . data , response . status , response . headers , config ) ;
962
- } ) ;
963
- return promise ;
964
- } ;
981
+ promise . then ( function ( response ) {
982
+ fn ( response . data , response . status , response . headers , config ) ;
983
+ } ) ;
984
+ return promise ;
985
+ } ;
965
986
966
- promise . error = function ( fn ) {
967
- assertArgFn ( fn , 'fn' ) ;
987
+ promise . error = function ( fn ) {
988
+ assertArgFn ( fn , 'fn' ) ;
968
989
969
- promise . then ( null , function ( response ) {
970
- fn ( response . data , response . status , response . headers , config ) ;
971
- } ) ;
972
- return promise ;
973
- } ;
990
+ promise . then ( null , function ( response ) {
991
+ fn ( response . data , response . status , response . headers , config ) ;
992
+ } ) ;
993
+ return promise ;
994
+ } ;
995
+ }
974
996
975
997
return promise ;
976
998
0 commit comments