1

Using an interceptor in AngularJS how can console.log("finished AJAX request") when any request has completed?

I have been looking at interceptors and so far have the following but it fires on the start of the request not the end.

app.factory('myInterceptor', [function() {
    console.log("finished AJAX request") 

    var myInterceptor = {

    };

    return myInterceptor;
}]);

config:

app.config(function ($stateProvider, $urlRouterProvider, $httpProvider) {

$httpProvider.interceptors.push('myInterceptor'); 

etc

1 Answer 1

6

You need to put the console in the response function of the httpInterceptor

// register the interceptor as a service
  $provide.factory('myHttpInterceptor', function($q, dependency1, dependency2) {
    return {
      // optional method
      'request': function(config) {
        // do something on success
        return config;
      },

      // optional method
     'requestError': function(rejection) {
        // do something on error
        if (canRecover(rejection)) {
          return responseOrNewPromise
        }
        return $q.reject(rejection);
      },



      // optional method
      'response': function(response) {
        // do something on success
        console.log('I am done');
        return response;
      },

      // optional method
     'responseError': function(rejection) {
        // do something on error
        if (canRecover(rejection)) {
          return responseOrNewPromise
        }
        return $q.reject(rejection);
      }
    };
  });

  $httpProvider.interceptors.push('myHttpInterceptor');

Over here in the response method I have included the console log function call

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

Comments

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.