0
votes

This is my controller and I am adding header in the request but header is not added in my request.

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $http) {
  $scope.call = function() {
    $http({
      method: 'POST',
      url: 'http://localhost:8081/api/GetData',
      headers: {
        'token': '33c028f2-6eb6-11e8-ac8b',
        'user': 'admin',
        'Content-Type': 'application/json'
      }
    }).then(function(response) {
        //$scope.names = response.data;
        console.log(response.data);
      },
      function(response) {
        //  alert(response.data);
        //Second function handles error
        $scope.content = "Something went wrong";
      }
    );
  }
});

When I check my browser console it does not showing any header in the request.

Response headers (138 B) Allow POST,GET,OPTIONS,HEAD Content-Length
0 Date Fri, 29 Jun 2018 10:10:58 GMT Server Jetty(8.1.15.v20140411)

Request headers (438 B) Accept
text/html,application/xhtml+xm…plication/xml;q=0.9,/;q=0.8
Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5
Access-Control-Request-Headers content,token,user
Access-Control-Request-Method POST Connection keep-alive Host
localhost:8081 Origin null User-Agent Mozilla/5.0 (X11; Ubuntu;
Linu…) Gecko/20100101 Firefox/60.0

Please help me for why header is not setting in my request

1
Which version of angular you are using?Sudhir Ojha
Make sure the server is accepting CORS policy. If not, the headers will never be sentMarcus Höglund

1 Answers

0
votes

Make sure your cors its configured, according angular1 documentation you should be able to set headers like this:

     var req = {
     method: 'POST',
     url: 'http://example.com',
     headers: {
        'Content-Type': undefined
     },
     data: { test: 'test' }
     }

     $http(req).then(function(){...}, function(){...});