在 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');
在响应方法中,我包含了控制台日志函数调用
关于javascript - AngularJS HTTP 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25282033/