Injecting auth headers with angularjs and restangular

混江龙づ霸主 提交于 2019-12-07 06:40:21

问题


I am creating a RESTFUL webservice that is called from an angularjs app which uses the restangular service. The REST service requires that an auth token is sent with each request. How can I get this sent with each request using restangular?

Looking at the documentation I can see there is a setRequestInterceptor method that I can set in the config which looks like I might be able to use:

RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params) {
      return {
        element: element,
        params: params,
        headers: _.extend(headers, {Authorization: 'Basic ' +  base64.encode('sometoken')})
      };
    });

I've not been able to get this to actually even get called by putting it in my config. I don't know where RestangularProvider provider needs to be injected from to get htis to work? I'd also want to create an external service which reads the token that has been generated and I'm not sure where to put this. Is there an example of this working anywhere? I've only been able to find scenarios that use $ngresource.

Any help appreciated.


回答1:


This was just an error on my own part. I was trying to chain this rather than include it as part of the config. A good example:

http://plnkr.co/edit/d6yDka?p=preview



来源:https://stackoverflow.com/questions/18847262/injecting-auth-headers-with-angularjs-and-restangular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!