2015-09-02 18 views
6

Sto cercando di replicare questo ricciolo utilizzando angolare $ http.get,

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

ma non sono sicuro come impostare correttamente l'intestazione angolare

questo è come l'ho provato,

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

Quindi come impostare correttamente l'intestazione?

Grazie ps: Non sto utilizzando l'autenticazione di base.

+0

Perché si utilizza il 'Token [token] = 1111' in Angular e non' Authorization: Token token = "1111" '? –

+0

Questa è la mia domanda in realtà, come scrivere correttamente l'intestazione. Ho provato anche questo, '$ http.get ('http: // localhost: 3000/posts.json', { intestazioni: {" Autorizzazione: Token token = 1111 "})' ma ancora non funziona. –

+0

possibile duplicato di [Imposta intestazione HTTP per una richiesta] (http://stackoverflow.com/questions/11876777/set-http-header-for-one-request) – sulaiman

risposta

10

Se si desidera che l'intestazione di autorizzazione contenga Token token="1111", questo dovrebbe funzionare. Sembra che anche le parentesi non corrispondessero.

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

Scusa per la mia domanda ma questo approccio non espone il tuo Chiave API (utilizzando gli strumenti di sviluppo)? –

+0

Sì, lo farebbe. A seconda del caso d'uso, questo può o non può essere accettabile. –

Problemi correlati