0

I have the following code:

$http.get(url).success(function(response,status,header,config) {
    $scope.mymodel = response;
}

I want to check the http status and call a function. I have to do this change on about 100 http.get calls in whole project. So I want to skip this overriding the success function like this.

function success(response,status,header,config) {
    if(status!=200){
       //my stuff
    }
    super.success(response,status,header,config);
}

Another possibility is replace $http.get(url).success for $http.get(url).success2 in order to tell my mates: "From now on guys use $http.get(url).success2 is cooler!"

Update

I have already know is a deprecated function, I must use it because is a project requirement.

4
  • 7
    Check out the http interceptors. Commented Jan 20, 2016 at 13:00
  • You can parameter the $http service. Go on docs.angularjs.org/api/ng/service/$http and section "Interceptors". Commented Jan 20, 2016 at 13:00
  • worth noting, .success is deprecated, and .then should be used instead. Commented Jan 20, 2016 at 13:01
  • What do you wish to do if the status is 200? An interceptor would be perfect for minimizing duplication. However it will limit what exactly you can do when your in that if block. Commented Jan 20, 2016 at 14:59

2 Answers 2

1

You should use $httpProvider.interceptors to achieve this.

You can sniff the $http request and response between to do whatever you want and return the $q unscathed.

$httpProvider.interceptors.push(['$q', 'mathScriptLoadError', function($q, mathScriptLoadError) {
                        return {

                            requestError: function(rejection){                            
                                mathScriptLoadError.anyError(rejection);    
                                return $q.reject(rejection);
                            },

                            responseError: function(rejection){                            
                                mathScriptLoadError.anyError(rejection);
                                return $q.reject(rejection);
                            }
                        };
    }]);

In the above code I get the anyError function of the factory mathScriptLoadError to inspect the rejection and invoke some process based on values. But this doesn't disturb the $http.get at any level.

Sign up to request clarification or add additional context in comments.

Comments

0

Deprecation Notice

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

-- AngularJS $http Service API Reference -- Deprecation Notice

Tell your mates: "From now on guys use $http.get(url).then is cooler!"


For other readers:

Response Interceptors

The AngularJS framework provides three places to inject response interceptor functions.

XHR responses can be intercepted and modified globally, locally, or by class.

2 Comments

Please use comments for additional discussions related to the question.
@CharlieH Added information that answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.