禁用$ httpProvider.interceptors.push以获取特定网址

时间:2016-04-28 21:55:48

标签: angularjs http angular-http angular-http-interceptors

我在AngularJS中找到关于我的$ httpProvider的解决方案时遇到了问题。 我有一个Accessservice,它处理来自服务器的响应错误,此服务可以处理401,404 .. http错误并重定向到页面。

但我有一个特定的网址,我不会应用此AccessService。我怎么能处理这个?

Config.js:

$httpProvider.interceptors.push('MyService');

MyServices:

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请求禁用此拦截器。?

提前致谢!

1 个答案:

答案 0 :(得分:0)

这肯定是可能的,您只需将if包裹在更多条件逻辑中。

if (rejection.config.url.match(/*regextomatchroute*/) {
    //handle the match
} else {
    // your current ifs
}
相关问题