问题
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