AngularJS 1.6登录升级代码不再使用REST了吗?

时间:2018-02-16 14:40:53

标签: javascript angularjs rest spring-boot angularjs-1.6

我有一个使用AngularJS 1.5.8的项目,登录方法如下:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
    $http.get('user', {
        headers : {
            // setting the Authorisation Header
            'Authorization' : 'Basic ' + base64Credential
        }
    }).success(function(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }).error(function(error) {
        $scope.message = 'Login Failed!';
    });
};

这是使用带有此请求的弹簧启动从数据库获取信息

@RequestMapping("/user")
    public Principal user(Principal principal) {
        return principal;
    }

代码必须更新才能在AngularJS 1.6.8上运行,所以我一直在关注我在网上找到的教程等,现在有了这个:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
     $http({
          url: 'user',
          method: 'GET',
          headers : {
                // setting the Authorisation Header
                'Authorization' : 'Basic ' + base64Credential
            }
        })
        .then(function onSuccess(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }, function onError(res) {
        $scope.message = 'Login Failed!';
    });
};

问题是我一直在登录失败,但用户在数据库中,并且.success已被弃用,因此不知道我做错了什么帮助非常感谢?

1 个答案:

答案 0 :(得分:2)

看看这个:Why are angular $http success/error methods deprecated? Removed from v1.6?

成功和错误方法正在幕后做一些工作来解开response.data。所以你可能需要做

if (res.data.authenticated) ....