所有请求AngularJS $ HTTP自定义标题 [英] AngularJS $http custom header for all requests
本文介绍了所有请求AngularJS $ HTTP自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有任何的方式来配置所有$ HTTP请求头与添加自定义信息。类似的配置:
VAR配置= {头:{
'授权':'基本d2VudHdvcnRobWFuOkNoYW5nZV9tZQ ==',
接受:应用/ JSON; ODATA =冗长'
}
};
但对于所有$ HTTP调用,我会在不同的服务做出。我敢肯定,有一个解决方案:D.Thanks
解决方案
您可以创建一个 $ HTTP
拦截器来扩展你的头:
\r
\r\r
\r myapp.factory('HTT prequestInterceptor',函数(){\r
返回{\r
要求:功能(配置){\r
\r
config.headers ['授权'] ='基本d2VudHdvcnRobWFuOkNoYW5nZV9tZQ ==';\r
config.headers ['接受'] ='应用/ JSON; ODATA =冗长';\r
\r
返回配置;\r
}\r
};\r
});\r
\r
myapp.config(函数($ httpProvider){\r
$ httpProvider.interceptors.push('HTT prequestInterceptor');\r
});
\r
I was wondering if there is any way to configure all $http requests header with adding custom info. Something like config :
var config = {headers: {
'Authorization': 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==',
'Accept': 'application/json;odata=verbose'
}
};
But for all $http calls I will make in different services. I'm sure there is a solution :D.Thanks
解决方案
You can create a $http
interceptor to extend your header:
myapp.factory('httpRequestInterceptor', function () {
return {
request: function (config) {
config.headers['Authorization'] = 'Basic d2VudHdvcnRobWFuOkNoYW5nZV9tZQ==';
config.headers['Accept'] = 'application/json;odata=verbose';
return config;
}
};
});
myapp.config(function ($httpProvider) {
$httpProvider.interceptors.push('httpRequestInterceptor');
});
这篇关于所有请求AngularJS $ HTTP自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文