0

我正在制作一个移动应用程序,并且我有一个处理用户登录的控制器。它会进行 http 调用(当前是 localhost)来检查用户是否有效。出于某种原因,它不会触发 $http 调用,并且控制台没有说明它失败了。我是 AngularJS 的新手,我可能在我的代码中犯了一个初学者错误。我将 AngularJS 与 Ionic 框架一起使用。

这是我的登录控制器:

.controller('LoginCtrl', ['$scope', '$ionicPopup', 'User', '$window', '$http', '$templateCache', function($scope, $ionicPopup, User, $window, $http, $templateCache){
    $scope.authUser = function(){
        console.log('authUser initialized');
        var email = document.getElementById('User_email');
        var password = document.getElementById('User_password');
        console.log('email=' + email.value);
        console.log('password=' + password.value);

        console.log('logging in...');
        if(email.value == '' || password.value == ''){
            var alertPopup = $ionicPopup.alert({
                title: 'Failed to login',
                template: 'Both fields need to be filled.'
            });
            alertPopup.then(function(res){
                console.log('Both fields need to be filled.');
            });
        } else {
            console.log('Contacting API server...');
            $scope.$apply(function(){
                $http({
                    method: 'POST',
                    url: 'http://api.tb.local/api.php',
                    data: { 'action': 'user/auth', 'email': email.value, 'password': password.value },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    cache: $templateCache
                }).
                success(function(response){
                    console.log('Response success');
                    console.log(response);
                    if(response.valid){
                        console.log('Login success. Redirecting to welcome page...');
                        $window.location.href = '#/welcome';
                    } else {
                        var alertPopup = $ionicPopup.alert({
                            title: 'Failed to login',
                            template: 'Wrong credentials. Please try again.'
                        });
                        alertPopup.then(function(res){
                            console.log('Wrong credentials. Please try again.');
                        });
                    }
                }).
                error(function(response){
                    var requestFailDialog = $ionicPopup({
                        title: 'Internal Error',
                        template: 'The server encountered an unknown error.'
                    });
                    requestFailDialog.then(function(res){
                        console.log('Internal Error: ' + response);
                    });
                });
            });
        }
    }
}])

我在控制台中得到的最后一件事是Contacting API server....

我手动测试了我的 API,它返回了一个 json { "valid": true },就像它应该的一样。

4

1 回答 1

1

尝试cache: $templateCache从 $http 请求中删除。

于 2014-09-23T06:37:49.503 回答