7

Is it possible to prevent a request using angularjs interceptors ?

$provide.factory('myHttpInterceptor', function($q, someService) {
  return {
    'request': function(config) {
      // here I'd like to cancel a request depending of some conditions
    }
  }
});

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

2 Answers 2

16

In 1.1.5 and later you can use the 'timeout' property of the configuration object.

From the documentation:

timeout – {number|Promise} – timeout in milliseconds, or promise that should abort the request when resolved.

Simple example:

$provide.factory('myHttpInterceptor', function($q, someService) {
  return {
    'request': function(config) {

        var canceler = $q.defer();

        config.timeout = canceler.promise;

        if (true) {

            // Canceling request
            canceler.resolve();
        }

        return config;
    }
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');
2
  • Could you please explain what the line return config || $q.when(config); does ? Or rather: how can config evaluate to false - the code above that line assume that it is a valid config object ? Aug 14, 2015 at 13:10
  • 1
    You are correct. It should just be return config, since config.timeout = canceler.promise; wouldn't work if config is undefined anyway. I was sloppy and took the code from the old interceptor documentation, which only contains the line return config || $q.when(config); and added some logic above it. I will edit it. Thank you.
    – tasseKATT
    Aug 15, 2015 at 17:12
-2

I apply this logic which is working:

$scope.cancelRequest = 0;               // initilize taking variable
$scope.searchUser = function() {
    $scope.cancelRequest = 0;           // initilize taking variable
    var opts = {};
    $scope.userList = [];               //array in witch i store the list
    if($scope.searchFrind != ""){       // checking the value of the model is blank or their is some data
        opts.limit_size = 10; 
        ProfileService.searchUser(opts, function(data) {   // calling the service which call the http request
            if( $scope.cancelRequest == 0 ){    // checking the value of cancelRequest as if the request is late and we doesnot want it then it fall in the else case
                angular.forEach(data.data.users,function(user) {
                    $scope.userList.push(user);
                })
            }else{                          //when the cancelRequest is notequal 0 then this part run
                $scope.userList = [];       //empty the array
            }
        });
    }else{
        $scope.userList = [];
        $scope.cancelRequest = 1; //changing the value of cancelRequest to 1 so that the pending http request  after completion does not disturb the array or any model
    }
};
1
  • 1
    You're still making an actual HTTP request (and discarding its response, resulting in useless server load), which is not desired by OP. Sep 9, 2015 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.