问题描述
我的API使用Accept-Language头得到它的当前语言返回翻译JSON。别介意。
My API uses the Accept-Language header to get it's current language which returns the json translated. Never mind that.
我如何可以改变angularJS / Javasctipt头。我想这样的:
How can i change the header with angularJS/Javasctipt. I tried this:
$http.defaults.headers.post["Accept-Language"] = "bs-Latn-BA";
但它似乎没有工作呢,是否还有其他选择吗?
but it doesn't seem to work, are there any other alternatives?
推荐答案
对于每一个请求住在 $ httpProvider.defaults.headers.common
对象发送默认头
The default headers sent for every single request live in the $httpProvider.defaults.headers.common
object.
您可以更改或使用的.config()
函数为每一个请求增加这些头,像这样:
You can change or augment these headers using the .config()
function for every request, like so:
angular.module('myApp', [])
.config(function($httpProvider) {
$httpProvider.defaults.headers
.common['Accept-Language'] = 'bs-Latn-BA';
});
我们也可以操作使用 $ HTTP
对象的默认属性这些默认值在运行时。例如,要添加属性动态标题,我们可以设置标题属性像这样:
We can also manipulate these defaults at run time using the defaults property of the $http
object. For instance, to add a property for dynamic headers, we can set the header property like so:
$http.defaults
.common['Accept-Language'] = "bs-Latn-BA";
这篇关于变化Accept-Language头使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!