繁体   English   中英

AngularJS HTTP拦截器

[英]AngularJS HTTP interceptor

AngularJS中使用拦截器时,如果有任何请求完成,console.log(“ AJAX请求完成”)怎么办?

我一直在寻找拦截器,到目前为止有以下内容,但是它在请求的开始而不是结束时触发。

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

    var myInterceptor = {

    };

    return myInterceptor;
}]);

配置:

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

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

etc

您需要将控制台置于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');

在响应方法的此处,我包括了控制台日志功能调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM