如何在Restangular拦截器中获取GET参数

时间:2016-02-18 13:41:24

标签: angularjs interceptor restangular

我遇到了一件事,我可以从restangular拦截器中获取自定义参数

这里是使用restangular

的请求
<pre>
 var getSaveMetricsSelectOptions = function(organizationId, metricSetId) {
            return Restangular.one('metricsets', organizationId)
                .one('metricset', metricSetId)
                .get({skipAuth:true})
                .then(function(response) {
                    return response.data;
                }, handleError);
        };
</pre>

这里有一个parmeter名称 skipAuth 我想进入拦截器。因为我不想通过一个传递参数(&#34; skipAuth&#34;,true)。我不想将此skipAuth参数发送到服务器,只想检查拦截器。

这是我的拦截器代码

<pre>Restangular.addRequestInterceptor(
            function(elem, operation, what,url) { 
                Restangular.setBaseUrl(API_CONFIG.url);
                //Add token to all the URLs except 'client/login'
                $log.log(elem, operation, what, url);
                if (what !== 'client/login') {
                    //check for token in cookie and set it in URL
                    $log.log($cookies.get('token'));
                    var token = $cookies.get('token');
                    if (angular.isDefined(token)) {
                        Restangular.setRequestSuffix('/' + token);
                    } else {
                        //Client login issue - token not found
                        $log.log('Client login issue - token not found');
                    }

                    //Check for JWT Token and add it in Authorization Header
                    if(!elem.skipAuth) { 
                        var jwt_token = authService.getJwtToken();
                        if (jwt_token) {
                            var defaultHeaders = {Authorization: 'Bearer ' + jwt_token};
                            Restangular.setDefaultHeaders(defaultHeaders);
                        }
                    }
                }

                return elem;
            });

</pre>

1 个答案:

答案 0 :(得分:0)

使用完整拦截器

Restangular.addFullRequestInterceptor(function(elem, operation, what, url, headers, params, httpConfig) {
            Restangular.setBaseUrl(API_CONFIG.url);
            //Add token to all the URLs except 'client/login'
            $log.log(elem, operation, what, params);
            if (what !== 'client/login') {
                //check for token in cookie and set it in URL
                $log.log($cookies.get('token'));
                var token = $cookies.get('token');
                if (angular.isDefined(token)) {
                    Restangular.setRequestSuffix('/' + token);
                } else {
                    //Client login issue - token not found
                    $log.log('Client login issue - token not found');
                }

                //Check for JWT Token and add it in Authorization Header
                if (!params.skipAuth) {
                    if (!elem.skipAuth) {
                        var jwt_token = authService.getJwtToken();
                        if (jwt_token) {
                            var defaultHeaders = { Authorization: 'Bearer ' + jwt_token };
                            Restangular.setDefaultHeaders(defaultHeaders);
                        }
                    }
                }
            }
            return {
                element: elem,
                headers: headers,
                httpConfig: httpConfig
            };
        });