【问题标题】:angular $http GET request with authorization token header带有授权令牌标头的角度 $http GET 请求
【发布时间】:2015-11-27 09:15:02
【问题描述】:

我正在尝试使用 angular $http.get 复制此卷曲,

curl -H "Accept: application/json" http://localhost:3000/posts -H 'Authorization: Token token="1111"'

但我不确定如何正确设置角度标题

我就是这样尝试的,

    app.controller('newsCtrl', function($http, $scope){
      $scope.news =[];
      $http.get('http://localhost:3000/posts.json', {
        headers: {"Authorization": "Token[token]=1111"}).success(function(response){
        console.log(response)
      })
    })

那么如何正确设置header呢?

谢谢 ps:我没有使用基本认证。

【问题讨论】:

  • 你为什么在 Angular 中使用 Token[token]=1111 而不是 Authorization: Token token="1111"
  • 这实际上是我的问题,如何正确编写标题。我也试过了,$http.get('http://localhost:3000/posts.json', { headers: {"Authorization: Token token=1111"}) 但还是不行。
  • Set HTTP header for one request 的可能重复项

标签: angularjs ionic


【解决方案1】:

如果您希望 Authorization 标头包含 Token token="1111",那么这应该可以。看起来你的括号也不匹配。

  $http.get('http://localhost:3000/posts.json', {
    headers: {
        "Authorization": 'Token token="1111"'
    }
  }).success(function(response){
    console.log(response)
  });

【讨论】:

  • 抱歉我的问题,但这种方法不会暴露您的 API 密钥(使用开发人员工具)吗?
  • 是的,会的。根据您的用例,这可能是可接受的,也可能是不可接受的。
  • @AnidMonsur 您能否详细说明为什么这会暴露令牌?如果使用 TLS,这仍然是真的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2018-10-12
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多