我可以使用AngularJS更改Accept-Language Request Header

时间:2022-06-15 15:06:40

Is there a way to change or edit the accept Language header that i send to my API ? is there a way in javascript Jquery or Angular? i don't want to send the default one, but the one my Cookie has!

有没有办法更改或编辑我发送到我的API的接受语言标题?在javascript Jquery或Angular中有一种方法吗?我不想发送默认的,但我的Cookie有!

2 个解决方案

#1


3  

In AngularJS you can set common headers by using $httpProvider and you can get the cookies by using $cookies service.

在AngularJS中,您可以使用$ httpProvider设置公共标头,您可以使用$ cookies服务获取cookie。

For example:

var app = angular.module("app", []);

app.config(["$httpProvider", "$cookies", function($httpProvider, $cookies) {
    // set Accept-Language header on all requests to
    // value of AcceptLanguageCookie cookie
    $httpProvider.defaults.headers.common["Accept-Language"] = $cookies.get("AcceptLanguageCookie");

    // or set headers on GET requests only
    if (!($httpProvider.defaults.headers).get) {
        ($httpProvider.defaults.headers).get = {};
    }
    $httpProvider.defaults.headers.get["Test-Header"] = "value";
}]);

#2


1  

To reference the cookie which contains your specific "Accept-Language" value you will want to do this at run time. The reason is that the app config only accepts Providers and the reference to Services is not recommended.

要引用包含特定“Accept-Language”值的cookie,您需要在运行时执行此操作。原因是app config仅接受Providers,不建议引用Services。

To do this at run time you will want to make use of the $http and $cookies services

要在运行时执行此操作,您需要使用$ http和$ cookies服务

Here's an example:

这是一个例子:

var app = angular.module("app", []);

app.run(["$http", "$cookies", function($http, $cookies){
  // set Accept-Language header on all requests
  $http.defaults.headers.common["Accept-Language"] = $cookies.get("LocaleCookie");
}]);

To statically set the "Accept-Language" WITHOUT a cookie then you can use .config()

要在没有cookie的情况下静态设置“Accept-Language”,您可以使用.config()

var app = angular.module("app", []);

app.config(["$httpProvider", function($httpProvider) {
  // set Accept-Language header on all requests
  $httpProvider.defaults.headers.common["Accept-Language"] = "fr";
}]);

#1


3  

In AngularJS you can set common headers by using $httpProvider and you can get the cookies by using $cookies service.

在AngularJS中,您可以使用$ httpProvider设置公共标头,您可以使用$ cookies服务获取cookie。

For example:

var app = angular.module("app", []);

app.config(["$httpProvider", "$cookies", function($httpProvider, $cookies) {
    // set Accept-Language header on all requests to
    // value of AcceptLanguageCookie cookie
    $httpProvider.defaults.headers.common["Accept-Language"] = $cookies.get("AcceptLanguageCookie");

    // or set headers on GET requests only
    if (!($httpProvider.defaults.headers).get) {
        ($httpProvider.defaults.headers).get = {};
    }
    $httpProvider.defaults.headers.get["Test-Header"] = "value";
}]);

#2


1  

To reference the cookie which contains your specific "Accept-Language" value you will want to do this at run time. The reason is that the app config only accepts Providers and the reference to Services is not recommended.

要引用包含特定“Accept-Language”值的cookie,您需要在运行时执行此操作。原因是app config仅接受Providers,不建议引用Services。

To do this at run time you will want to make use of the $http and $cookies services

要在运行时执行此操作,您需要使用$ http和$ cookies服务

Here's an example:

这是一个例子:

var app = angular.module("app", []);

app.run(["$http", "$cookies", function($http, $cookies){
  // set Accept-Language header on all requests
  $http.defaults.headers.common["Accept-Language"] = $cookies.get("LocaleCookie");
}]);

To statically set the "Accept-Language" WITHOUT a cookie then you can use .config()

要在没有cookie的情况下静态设置“Accept-Language”,您可以使用.config()

var app = angular.module("app", []);

app.config(["$httpProvider", function($httpProvider) {
  // set Accept-Language header on all requests
  $httpProvider.defaults.headers.common["Accept-Language"] = "fr";
}]);