AngularJS : How to prevent a request

后端 未结 2 842
臣服心动
臣服心动 2020-12-20 17:29

Is it possible to prevent a request using angularjs interceptors ?

$provide.factory(\'myHttpInterceptor\', function($q, someService) {
  return {
    \'reque         


        
2条回答
  •  被撕碎了的回忆
    2020-12-20 17:57

    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');
    

提交回复
热议问题