为特定 url 禁用 $httpProvider.interceptors.push
Disable $httpProvider.interceptors.push for a specific url
我在 AngularJS 中找到有关我的 $httpProvider 的解决方案时遇到问题。
我有一个处理来自服务器的响应错误的访问服务,该服务可以处理 401,404 .. http 错误并重定向到页面。
但我有一个特定的 url 我不会应用此 AccessService 。我该如何处理?
Config.js :
$httpProvider.interceptors.push('MyService');
我的服务:
angular.module('myapp').factory('MyService', function ($q, $location) {
return {
responseError: function (rejection) {
var code = rejection.status;
if (code === 401)
$location.url('/auth');
if (code === 403)
$location.url('/unauthorized');
return $q.reject(rejection);
}
};
});
在我的控制器中:
$scope.sendForm = function(){
CustomService.posting($scope.form).then(function(data){
if(data.status == 200){
// code when 200
// no problem here
}
if(data.status == 415)
// line of code
if(data.status == 401)
// line of code
});
};
有一种方法可以为特定控制器或 http 请求禁用此拦截器。?
提前致谢!
这当然是可能的,你只需要用更多的条件逻辑包装你的 if。
if (rejection.config.url.match(/*regextomatchroute*/) {
//handle the match
} else {
// your current ifs
}
我在 AngularJS 中找到有关我的 $httpProvider 的解决方案时遇到问题。 我有一个处理来自服务器的响应错误的访问服务,该服务可以处理 401,404 .. http 错误并重定向到页面。
但我有一个特定的 url 我不会应用此 AccessService 。我该如何处理?
Config.js :
$httpProvider.interceptors.push('MyService');
我的服务:
angular.module('myapp').factory('MyService', function ($q, $location) {
return {
responseError: function (rejection) {
var code = rejection.status;
if (code === 401)
$location.url('/auth');
if (code === 403)
$location.url('/unauthorized');
return $q.reject(rejection);
}
};
});
在我的控制器中:
$scope.sendForm = function(){
CustomService.posting($scope.form).then(function(data){
if(data.status == 200){
// code when 200
// no problem here
}
if(data.status == 415)
// line of code
if(data.status == 401)
// line of code
});
};
有一种方法可以为特定控制器或 http 请求禁用此拦截器。?
提前致谢!
这当然是可能的,你只需要用更多的条件逻辑包装你的 if。
if (rejection.config.url.match(/*regextomatchroute*/) {
//handle the match
} else {
// your current ifs
}