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