在控制器中设置defaultHeader的问题

时间:2015-10-01 14:15:54

标签: angularjs rest

每当我在auth-cntrl中设置defaultHeader进行基本身份验证时,它都是第一次运行。但是如果我刷新页面我的身份验证API会给出 “没有授权”错误。

app.controller('authCtrl',['$scope','Restangular','userServices','$location','$base64','cookie', '$mdToast', '$animate', function ($scope,Restangular,userServices,$location,$base64,cookie,$mdToast,$animate){
$scope.signIn = function() {
    var data = {
            "Username": $scope.username,
            "Password": $scope.password
    };
    credBase64 = $base64.encode(unescape(encodeURIComponent($scope.username +':'+ $scope.password)));
Restangular.setDefaultHeaders({'Authorization': 'Basic ' + credBase64});
    userServices.users(Restangular).post(data).then(function(response){
        if(response.success) {
            cookie.set('session',credBase64);
            $location.path('/workplace');
        }else {
            $location.path('/signin');
        }
    });
};
}]);

0 个答案:

没有答案