Angularjs 外部URL的Http拦截器

Angularjs 外部URL的Http拦截器,angularjs,Angularjs,我编写了一个http拦截器,将身份验证令牌添加到每个请求中。在我的html中,当我单击一个链接时,这个拦截器不会被调用。不知道为什么会这样 我的截取器代码- angular.module('demo', []) .factory('httpAuthorizationInterceptor',['$window', function ($window) { return { request: function (config) { if (confi

我编写了一个http拦截器,将身份验证令牌添加到每个请求中。在我的html中,当我单击一个链接时,这个拦截器不会被调用。不知道为什么会这样

我的截取器代码-

angular.module('demo', [])
.factory('httpAuthorizationInterceptor',['$window', function ($window) {
    return {
        request: function (config) {
            if (config.url.indexOf('login') == -1) {
                config.headers['Authorization'] = 'Session ' +<session_id>;
            }
            return config || $q.when(config);
        }
    };
}])
angular.module('demo',[])
.factory('httpAuthorizationInterceptor',['$window',函数($window){
返回{
请求:函数(配置){
if(config.url.indexOf('login')=-1){
config.headers['Authorization']='Session'+;
}
返回配置| |$q.when(配置);
}
};
}])
我的html-

<a data-ng-href="http://example.com/view"></a>

锚标记实际上不会进行ajax调用,而是用于拦截通过angle进行的ajax调用。单击锚定标记就像在浏览器中打开URL一样

为了进行ajax调用:您需要以以下方式调整代码:

angular.module('demo', [])
.config(['$httpProvider', function ($httpProvider) {

    var interceptor = [function() {
        return {
            'request': function(config) {
                if (config.url.indexOf('login') == -1) {
                    config.headers['Authorization'] = 'Session ' + <session_id>;
                }
                return config;
            }
        };
    }];

    $httpProvider.interceptors.push(interceptor);
}]);
您的HTML代码将是:

<a href="doSomething()"></a>

唯一的问题是,您正在进行ajax调用的外部url要么在同一个域上,要么必须支持跨源请求


希望这有帮助

谢谢你,沙尚克。您能建议在这种情况下添加授权令牌的最佳方式吗。我是否需要为锚定标记编写自己的指令。当然,您实际想要对该外部url做什么?您是想在该选项卡上执行ajax调用,还是只想在另一个选项卡中打开?Shashank,我想使用该选项卡执行ajax调用,并在执行ajax调用时传递授权令牌。我已经有一个http拦截器在每次调用时传递授权令牌。目前,我正在编写一个服务,它执行ajax调用,并且似乎工作正常。不知道这是不是最好的way@EshwariChandrashekhar我已经更新了答案。请看一看。谢谢@Shashank这正是我最后要做的。
<a href="doSomething()"></a>