拦截弹AngularJS [英] Interceptor in AngularJS

查看:110
本文介绍了拦截弹AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦与我的REST的应用程序设置标题。
我需要授权令牌设置授权头,实际上我用这个code:

  app.factory('sessionInjector',['$日志,$ rootScope',函数($日志,$ rootScope){
    VAR sessionInjector = {
        要求:功能(配置){                $ log.debug($ rootScope.accesstoken);                angular.forEach($ rootScope.accesstoken,功能(值,键){
                    如果(键=='ACCESS_TOKEN'){
                        $ log.debug(值);
                        config.headers ['授权'] =旗手+价值;
                    }
                });            返回配置;
        }
    };
    返回sessionInjector;
}]);
的app.config(['$ httpProvider',函数($ httpProvider){
    $ httpProvider.interceptors.push('sessionInjector');
}]);

在的forEach价值有权标记值。

遗憾的是仍然没有得到资源...

日志记录语句执行,我看不出请求头。

首先请求头是获得令牌,它的做工精细,但对于像第二个请求资源的请求不发送适当的头。
对于卷曲这个作品:

 卷曲-H授权:承载5314f45f-0ad0-4f3c-92c0-429c8075eea4的http://本地主机:8080 /分类/


解决方案

一天痛苦后,我发现,当我在IE浏览器执行现场实施拦截工作......但是,为什么?为什么我不能设置头Chrome或Firefox下?我试着设定interceptorsm头有人士Himanshu的方法,并使用AJAX语法,为什么所有的人都在这些浏览器不工作?

I have some troubles with setting header for my REST app. I need to set authorization token to authorization header and actually i use this code:

app.factory('sessionInjector', ['$log','$rootScope', function($log, $rootScope) {  
    var sessionInjector = {
        request: function(config) {

                $log.debug($rootScope.accesstoken);

                angular.forEach($rootScope.accesstoken, function(value, key) {
                    if(key=='access_token'){
                        $log.debug(value);
                        config.headers['Authorization'] = "Bearer " + value;
                    }
                });

            return config;
        }
    };
    return sessionInjector;
}]);


app.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.interceptors.push('sessionInjector');
}]);

value in forEach has right token value.

Unfortunately still don't getting resources...

Logging statements are executed, i don't see header in request. First request header is for getting token, and it's work fine, but requests for resources like second request don't send appropriate header. For CURL this works:

curl -H "Authorization: Bearer 5314f45f-0ad0-4f3c-92c0-429c8075eea4" http://localhost:8080/categories/

解决方案

After one day in pain I found that implemented interceptor is working when I execute site on IE... But why ? Why I cannot set headers under Chrome or Firefox ? I tried set headers with interceptorsm with method of Himanshu and with AJAX syntax, why all of them doesnt work on these browsers ?

这篇关于拦截弹AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆