【问题标题】:Disable $httpProvider.interceptors.push for a specific url为特定 url 禁用 $httpProvider.interceptors.push
【发布时间】:2016-04-29 18:10:58
【问题描述】:

我无法在 AngularJS 中找到关于我的 $httpProvider 的解决方案。 我有一个 Accessservice,我处理来自服务器的响应错误,该服务可以处理 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 请求禁用此拦截器。?

提前致谢!

【问题讨论】:

  • 在 AccessService 中,检查 URL,如果是特定的 URL,则不执行任何操作。
  • 怎么样?这一行 $httpProvider.interceptors.push('AccessService');写在 config.js 中
  • 正如我所说:在 AccessService 中。不在将其添加到拦截器的代码中。
  • 我编辑了帖子,请您重新阅读帖子。告诉我你的方法。谢谢
  • var url = rejection.config.url; if (url === specificUrl) return $q.reject(rejection);

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


【解决方案1】:

这当然是可能的,你只需要将你的 if 包装在更多的条件逻辑中。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2019-02-04
    • 1970-01-01
    • 2019-12-16
    • 2016-11-27
    相关资源
    最近更新 更多