0

I am very new to angularJS.

My Backend is DRF and I have successfully implemented token.

this is my token:

{
    "key": "217c3b5913b583a0dc3285e3521c58b4d7d88ce2"
}

Before I implement token in backend, it was working nice:

$scope.getAllContact = function() {
    var data = $http.get("http://127.0.0.1:8000/api/v1/contact")
    .then(function(response) {
      $scope.contacts = response.data;
    });
  };

But, now I am not getting how can I implement this token here

Can anyone help me in this case?

tanmay
  • 7,761
  • 2
  • 19
  • 38
  • 1
    Try setting the token in the headers? https://docs.angularjs.org/api/ng/service/$http#setting-http-headers – Claies May 27 '19 at 05:53
  • Yes, i notice this before but i am not getting how to implement token header –  May 27 '19 at 05:57

1 Answers1

0

Try to use this. You need to attach the token in the headers.

$http({
   url : "http://127.0.0.1:8000/api/v1/contact",
   method : 'GET',
  headers : {
      'Content-Type' : 'application/json',    
      'key': "217c3b5913b583a0dc3285e3521c58b4d7d88ce2"
  }
}).then(function(response){
    $scope.contacts = response.data;
});

Note that, this is binding the token to only this request. Use $http interceptors to add the token to each request that you make. See here: Angular Js - set token on header default

Harshad
  • 134
  • 1
  • 6