AngularJS 具有选择性的 http 拦截器 url
AngularJS http interceptor with selective url
如何让 $http 拦截器只对给定的模式作出反应?
例如,我希望拦截器处理每个“/api/*”请求并单独处理其他请求。
您可以在请求或响应中过滤 url 成功或拒绝函数。
假设您需要处理以 "math/".
开头的 url 的请求和响应的错误
这是你的拦截器。
$httpProvider.interceptors.push(function($q, mathError) {
return {
requestError: function(rejection){
mathError.anyError(rejection);
return $q.reject(rejection);
},
responseError: function(rejection){
mathError.anyError(rejection);
return $q.reject(rejection);
}
};
});
这是你处理它的工厂
myApp.factory('mathError', function(){
return {
anyError: function(rejection){
if (rejection.config.url.substr(0, 5) === "math/") {
console.log("Only Math errors are handled here");
//Do whatever you need here
}
}
});
如何让 $http 拦截器只对给定的模式作出反应?
例如,我希望拦截器处理每个“/api/*”请求并单独处理其他请求。
您可以在请求或响应中过滤 url 成功或拒绝函数。
假设您需要处理以 "math/".
开头的 url 的请求和响应的错误这是你的拦截器。
$httpProvider.interceptors.push(function($q, mathError) {
return {
requestError: function(rejection){
mathError.anyError(rejection);
return $q.reject(rejection);
},
responseError: function(rejection){
mathError.anyError(rejection);
return $q.reject(rejection);
}
};
});
这是你处理它的工厂
myApp.factory('mathError', function(){
return {
anyError: function(rejection){
if (rejection.config.url.substr(0, 5) === "math/") {
console.log("Only Math errors are handled here");
//Do whatever you need here
}
}
});