Tôi biết cách đánh chặn TẤT CẢ các yêu cầu, nhưng tôi chỉ muốn chặn các yêu cầu từ tài nguyên của mình.Làm thế nào để sử dụng chặn interularJS để chỉ chặn các yêu cầu http cụ thể?
Có ai biết cách thực hiện việc này không?
services.config(['$httpProvider',function($httpProvider) {
$httpProvider.interceptors.push('myHttpInterceptor');
}]);
services.factory("userPurchased", function ($resource) {
return $resource("/api/user/purchases/:action/:item",
{},
{
'list': {method: 'GET', params: {action: 'list'}, isArray: false},
'save': {method: 'PUT', params: {item: '@item'}},
'remove': {method: 'DELETE', params: {item: '@item'}},
}
);
});
services.factory('myHttpInterceptor', function($q,$rootScope) {
// $rootScope.showSpinner = false;
return {
response: function(response) {
$rootScope.showSpinner = false;
// do something on success
console.log('success');
console.log('status', response.status);
//return response;
return response || $q.when(response);
},
responseError: function(response) {
// do something on error
$rootScope.showSpinner = true;
console.log('failure');
console.log('status', response.status)
//return response;
return $q.reject(response);
}
};
});
Cảm ơn bạn - đây chính xác là những gì tôi đang tìm kiếm! – jrutter
Chính xác những gì tôi cần! Cảm ơn! –
Điều này không hoạt động khi tôi đẩy nhiều trình chặn vào httpProvider. Tôi muốn gọi chỉ là một interceptor riêng biệt, mà tôi đang làm như trên, nhưng phương pháp yêu cầu được gọi của tất cả các interceptors tôi đã đăng ký. – Sanket