4

I'm trying to convert a module written using jQuery to AngularJS.

I have an ajaxSuccess and ajaxError handler which does an ajax response processing at the global level. It responsible for showing success/failure messages across all ajax requests.

Is there a equivalent for this in AngularJS?

I've gone through the $http service, but haven't found any solutions.

Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • this question is so significant that I was looking for the solution. I have got one another [**SO question here**](http://stackoverflow.com/questions/22284111/php-jquery-ajax-call-throws-neterr-empty-response) which fails due to "provisional headers sent". Could you please solve it? thanks for others too. – webblover Mar 20 '14 at 16:58

1 Answers1

3

You can achieve this with interceptors. Example:

    myapp.config(function($httpProvider) {
      var myInterceptor = ['$rootScope','$q', function(scope, $q) {

        function onSuccess(response) {
          return response;
        }

        function onError(response) {
          return $q.reject(response);
        }

        return function(promise) {
          return promise.then(onSuccess, onError);
        };

      }];

      $httpProvider.responseInterceptors.push(myInterceptor);
    });

This will capture all your angular $http and $resource calls and call onError or onSuccess before continuing with your custom callbacks.

Stewie
  • 60,366
  • 20
  • 146
  • 113