2015-12-30 97 views
0

我正在jwt angular + java球衣認證。我能夠將jwt令牌從服務器發送回客戶端,在Chrome控制檯中可以將其視爲狀態碼爲200的POST響應。錯誤捕獲JWT

我的問題是我不能將它保存在一個變量,以便我可以執行解碼。我得到一個錯誤這樣的:

TypeError: href is null 
e/<()angular.min.js (line 107) 
Ze/this.$get</<()angular.min.js (line 81) 
f/<()angular.min.js (line 119) 
lf/this.$get</r.prototype.$eval()angular.min.js (line 133) 
lf/this.$get</r.prototype.$digest()angular.min.js (line 130) 
lf/this.$get</r.prototype.$apply()angular.min.js (line 134) 
g()angular.min.js (line 87) 
T()angular.min.js (line 92) 
Uf/</w.onload()angular.min.js (line 93) 
stackFrame.js (line 357) 
<System> 
error is:undefined 

這裏是我的代碼:

myApp.controller('loginController', ['$scope', '$http', function ($scope, $http) 
{ 
$scope.email = ""; 
$scope.password = ""; 

$scope.loginForm = function() { 
    var data = {email: $scope.email, password: $scope.password}; 
    var url = 'rs/loginResource'; 
     $http.post(url, data).success(function (response) 
     { 
      console.log("token is: " + response); 
     }).error(function (error) 
     { 
      console.log("error is:" + error); 
     }); 
}; 
}]); 

回答

0

你的JSON發送令牌?或作爲純文本?

我最近在HAM堆棧上使用了基於jwt的認證,並且在對象中提供了令牌,所以響應類似於{token:'asdasds.sdadad.asdad'},我通常以以下形式在Angular中調用服務器:

$http({ 
    method: 'POST', 
    url: 'rs/loginResource', 
    data: { 
     email: $scope.email, 
     password: $scope.password 
    } 
}) 
.then(function (resp) { 
    var token = resp.data.token 
}) 
.catch(function(error){ 
    return error; 
}); 
+0

我使用'Response.ok(token).build();'jersery方法發送令牌。我猜jax-rs球衣的自動響應生成器功能導致了這個問題。令牌處於這樣的形式只是來:'eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhbGljZSIsImlzcyI6Imh0dHBzOlwvXC9jMmlkLmNvbSIsImV4cCI6MTQ1MTQxNDU0MH0 .HvguthVdVZ8IuP6Hsko95I70jB41jpZLqAC4VAmphDJcnRwdBBkeVBVS8RNP9z1J1c7bjzeXL5gaufr6P02GhoMwh37l58Sxzwu 8dyxLZOyyEI7A03yZkmXzRKi_TPPG-vQR6Ya9omUPioG1mPU9ZOI4XTuZooRyxph8uh9JJfw' – kittu

+0

你使用Java的球衣作爲後端? – kittu

+0

不,我在Nodejs上使用Hapijs作爲後端,但實際上並不重要,唯一重要的是響應對象的外觀如何,何時以角度接收它。我只注意到你正在使用.success(函數(響應))來接收已解決的承諾。你可以用.then替換它嗎(函數(響應))? –