$ HTTP认证头在AngularJS [英] $http Auth Headers in AngularJS
本文介绍了$ HTTP认证头在AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个打一个节点API的角度应用程序。我们的后端开发人员实现对API的基本身份验证,我需要在我的请求发送AUTH头。
I have an angular application that is hitting a node API. Our backend developer has implemented basic auth on the API, and I need to send an auth header in my request.
我已经找到了:
$http.defaults.headers.common['Authorization'] = 'Basic ' + login + ':' + password);
我试过:
.config(['$http', function($http) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + login + ':' + password);
}])
除了直接附加它要求:
As well as appending it directly to the request:
$http({method: 'GET', url: url, headers: {'Authorization': 'Basic auth'}})})
但没有任何工程。如何解决此问题?
But nothing works. How to solve this?
推荐答案
您正在混合使用案例;实例化服务( $ HTTP
)无法在配置阶段使用,而供应商也不会在工作运行块。从模块文档:
You're mixing the use cases; instantiated services ($http
) cannot be used in the config phase, while providers won't work in run blocks. From the module docs:
- 配置块 - […]只有供应商和常量可以被注入
成配置模块。这是prevent意外实例
服务之前就已经完全配置。 - 生成块 - […]只有实例和常量可注入运行
块。这是prevent进一步的系统配置过程
应用程序的运行时间。
- Configuration blocks - […] Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
- Run blocks - […] Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.
所以使用下列的:
app.run(['$http', function($http) {
$http.defaults.headers.common['Authorization'] = /* ... */;
}]);
app.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.common['Authorization'] = /* ... */;
}])
这篇关于$ HTTP认证头在AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文