【问题标题】:AngularJS : Implementing token in $httpAngularJS:在 $http 中实现令牌
【发布时间】:2019-05-27 14:25:14
【问题描述】:

我对 angularJS 很陌生。

我的后端是 DRF,我已经成功实现了令牌。

这是我的令牌:

{
    "key": "217c3b5913b583a0dc3285e3521c58b4d7d88ce2"
}

在我在后端实现令牌之前,它运行良好:

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

但是,现在我不知道如何在这里实现这个令牌

在这种情况下谁能帮助我?

【问题讨论】:

标签: angularjs


【解决方案1】:

尝试使用它。您需要在标头中附加令牌。

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

请注意,这只是将令牌绑定到此请求。使用$http 拦截器将令牌添加到您发出的每个请求中。 见这里:Angular Js - set token on header default

【讨论】:

  • 有什么问题?
  • 检查令牌是否在开发者工具的“网络”选项卡中发送。
  • 我现在正在尝试以另一种方式解决它,例如当用户登录时,它会生成一个登录名,并且会在 GET 方法中附带令牌
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-27
  • 2016-01-24
  • 2017-06-17
  • 2016-06-26
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
相关资源
最近更新 更多