AlCode
AlCode

Reputation: 565

Change Accept-Language Header with javascript

My API uses the Accept-Language header to get it's current language which returns the json translated. Never mind that.

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?

Upvotes: 1

Views: 4680

Answers (2)

Endre Simo
Endre Simo

Reputation: 11541

The default headers sent for every single request live in the $httpProvider.defaults.headers.common object.

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';
});

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";

Upvotes: 4

ooozguuur
ooozguuur

Reputation: 3476

AngularJS you can set common headers by using $httpProvider. Angularjs

Example:

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

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

Upvotes: 2

Related Questions