-1

I have the below piece of code used for a bootstrap typeahead.

return $http.post(searchClients, {
      params: {
            clientName: val,
     }
}).then(function(response){
    return response.data.results;
   });

I need to add a token to the header. How do I go about adding the token to the header?

searchClients holds the URL which is defined elsewhere. this code works, I just need to know on how to some extra info in the header.

Ela Buwa
  • 1,652
  • 3
  • 22
  • 43
  • Did you try : headers: {'Content-type': 'application/json', 'Authorization': 'Bearer ' + window.localStorage.getItem('token') }, – Nikita Sep 12 '17 at 06:16
  • Try to connect the service that tells your [HTTP headers](http://www.reliply.org/tools/requestheaders.php) and capture (just print plain HTML) the output. At least you will know if your headers are really lost on the way or maybe something else – MariaJayam Selvanayagam Sep 12 '17 at 06:17

1 Answers1

1
    $http({
    method: 'POST',
     headers: {
        'Content-Type': 'application/json;charset=UTF-8',
         'X-TOKEN': '9AF4E2526279434180C7365EF86F6E77'
    },
    url: 'http://xxx/User/Validate?UserName='+username+'&Password='+password+,

    data: {username:username, password:password}
}).then(function successCallback(response) {
    console.log(response);
     var logininfo = (response);
     localStorage.setItem("user_info", JSON.stringify(logininfo));
     $state.go('home');
}, function errorCallback(response) {
     console.log(response)
    if(response.status = 401){ //
       $scope.showpopup1("Message","invaild Username && Password");
    }
});