带有授权令牌头的angular $ http GET请求

时间:2022-08-22 19:34:40

I am trying to replicate this curl using angular $http.get,

我试图使用angular $ http.get复制这个卷曲,

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

but I am not sure how to properly set the angular header

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

this is how I tried it,

这是我试过的方式,

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

So how do I properly set the header?

那么如何正确设置标题?

Thank you ps: I am not using Basic authentication.

谢谢ps:我没有使用基本身份验证。

1 个解决方案

#1


10  

If you want the Authorization header to contain Token token="1111", then this should work. It looks like your brackets were not matching up also.

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

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

#1


10  

If you want the Authorization header to contain Token token="1111", then this should work. It looks like your brackets were not matching up also.

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

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