1
내 질문은 this question의 더 크고 광범위한 버전입니다. AngularJS 기능 내에서 http 요청을 가로 채고 싶습니다. 나중에 요청 URL을 변경하고 서버에 전달해야합니다.AngularJS를 사용하여 모든 HTTP 요청을 차단하는 방법은 무엇입니까?
어떻게하면됩니까? 지금까지 $ httpProvider 및 $ q를 사용하여 인터셉터를 만들었지 만 누군가가 내 페이지의 href 링크 등을 클릭하면 모든 요청이 아닌 $ http 요청 만 가로 채기 만 할 수 있습니다.. 내 인터셉터 코드는 다음과 같습니다. -
// register the interceptor as a service
myModule.factory('myHttpInterceptor', function ($q) {
return {
// optional method
'request': function (config) {
// do something on success
console.log("request success");
return config;
},
// optional method
'requestError': function (rejection) {
// do something on error
console.log("Request Error");
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
},
// optional method
'response': function (response) {
// do something on success
console.log("Response received");
return response;
},
// optional method
'responseError': function (rejection) {
// do something on error
if (canRecover(rejection)) {
return responseOrNewPromise
}
return $q.reject(rejection);
}
};
});
myModule.factory('myInterceptor', ['$log', function ($log) {
$log.debug('$log is here to show you that this is a regular factory with injection');
var myInterceptor = {
};
return myInterceptor;
}]);
myModule.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push('myHttpInterceptor');
}]);