AngularJs:对HTTP请求事件监听器 [英] AngularJs : event listener on http requests

查看:1336
本文介绍了AngularJs:对HTTP请求事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我使用的OAuth2来保护我的API,我需要之前的任何HTTP requets获得新的访问令牌,如果previous访问令牌已过期。

Since i'm using Oauth2 to protect my Api, i need to get a new access token before any http requets if the previous access token has expired.

我没用过事件侦听多到现在。

I didn't used event listener much until now.

下面我做了什么,现在(请让我知道这是否是正确的):

Here what i did for now (Please let me know if it is correct) :

ApplicationController.js

app.controller('ApplicationController', function($rootScope, $scope, $localStorage, AuthService){

    // Listening event apiRequested
    $scope.$on('event:apiRequested', function(e) {

        AuthService.token();
        // Restore the access_token in case it has changed
        access_token = $localStorage.getObject('access_token');

    });

})

UserController.js:

$rootScope.$broadcast('event:apiRequested');

// Get Users around
return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
    return response;
});

第一件事,我不知道关于...请问$ HTTP事件是否已经完全执行的处理?

First thing i'm not sure about ... Does $http is processed if the event already executed entirely?

所以,因为我不知道,我正在考虑增加一个回调。

So since i'm not sure, i'm thinking about adding a callback.

下面的想法:

$rootScope.$broadcast('event:apiRequested', function(response){

    if(response){

        // Get Users around
        return $http.post(domain+'/api/users?access_token='+access_token.key, data).then(function(response){
            return response;
        });

    }

});

请让我知道是否有可能做到这一点还是我应该使用比事件监听器别的东西该案例。

Please let me know if it is possible to do that or should i use something else than event listener for that case.

推荐答案

你为什么不使用的 的拦截器是为了拦截HTTP请求?
在你的情况,你要添加这个非常具体的行为纳入请求的一部分。

Why don't you use interceptors that is done to intercept HTTP request ? In your case, you shall add this very specific behaviour into the "request" part.

看到一个拦截器为例波纹管:

See an interceptor exemple bellow:

var $myService; // Add a constant that store the service
    $httpProvider.interceptors.push(['$location', '$injector', '$q', function($location, $injector, $q) {
                             return {

                               'request' : function(config){
                                 console.log("intercept request", config.url,config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                               'response' : function(config){
                                 $myService= $myService|| $injector.get('$myService'); // inject the service manually if constant is undefined
                                 console.log("intercept response", config)
                                 // Your token shall be retreive in this part
                                 return config
                               },
                                 'responseError': function(rejection) {

                                     console.log("responseError intercepted" , rejection);
                                      if (rejection.status === 403) {

                                         return $q.reject(rejection);

                                     } else if (rejection.status === 423) {



                                         return $q.reject(rejection);
                                     }else
                                          return $q.reject(rejection);
                                 }
                             };
                         }]);

拦截器将被定义为的.config([$ httpProvider功能($ httpProvider)

这篇关于AngularJs:对HTTP请求事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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