设置angularjs $ HTTP头全球 [英] Setting angularjs $http headers globally
本文介绍了设置angularjs $ HTTP头全球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多个控制器web应用程序。我在我的控制器之一(通过 http.defaults.headers.common ['设置
)。然而,这些头没有得到来自其他控制器的后续调用设置。我必须将它们设置为每个控制器我还是不够一次? $ HTTP
服务的默认标题在回调headername']
I have a webapp with multiple controllers. I'm setting the defaults headers of the $http
service in a callback in one of my controllers (via http.defaults.headers.common['headername']
). However, those headers do not get set in subsequent calls from other controllers. Do I have to set them for each controller I have or is one time enough?
推荐答案
您应该使用以下两种方法之一:
You should use one of two methods:
在运行段设置$ http.defaults.headers例如
Set $http.defaults.headers in run block e.g.
module.run(function($http) {
$http.defaults.headers.common.Authorization = 'Basic Token';
});
使用拦截器
var interceptor = function() {
return {
'request': function(config) {
config.headers['Authorization'] = 'Basic Token';
}
}
};
angular.module('app', [])
.config(function ($httpProvider) {
$httpProvider.interceptors.push(interceptor);
});
这篇关于设置angularjs $ HTTP头全球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文