AngularJS:登录功能在PHP代码中不起作用

时间:2016-03-26 12:24:20

标签: javascript php angularjs

目前,我正在尝试使用AngularJS框架创建登录功能。我尝试创建一个登录函数,但它执行else语句。我可以知道如何解决这个问题。

这是我的login.php代码:

$params = json_decode(file_get_contents('php://input'),true);

if(!isset($_SESSION)){ 
    session_start(); 
}

include "classes/filmService.class.php";
$db = pdoDB::getConnection();
header("Content-Type: application/json");

if(isset($params['userid']) && isset($params['password'])){

    $userid   = $params['userid'] ;
    $password = $params['password'];
    $service  = new FilmService();
    $result   = $service->loginRob($userid,$password);
    echo $result;
}else{
    echo '{"status": "fail", "message": "Please enter email & password la!!."}';
}

这是我的services.js代码:

this.login = function (userID, passwd) {
            var defer = $q.defer(),
            data = {
              //action: 'loginRob',
              userid: userID,
              password: passwd
            };

            $http({
                method: 'POST',
                url: loginUrl,
                params: data,
                cache: true,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}

            }). // notice the dot to start the chain to success()
            success(function (response) {
                defer.resolve({
                    data: response.status, // create data property with value from response
                    result: response,
                    user: response.username
                });
                console.log(response);
            }). // another dot to chain to error()
            error(function (err) {
                defer.reject(err);
            });
            // the call to getCourses returns this promise which is fulfilled 
            // by the .get method .success or .failure
            return defer.promise;
        };

这是我的controller.js代码:

.controller('LoginController',
            [
                '$scope',
                'dataService',
                '$location',
                '$window',
                '$rootScope',
                function ($scope, dataService, $location, $window, $rootScope){
                    $scope.check_login = function($event,userID,passwd){
                        if(userID && passwd){
                            dataService.login(userID,passwd).then(
                                function (response){
                                    if(response.result.status=='ok'){
                                        $scope.user = response.user;
                                        $rootScope.$broadcast("passuser", $scope.user);
                                    }else{
                                        $scope.message = response.result.message;

                                    }
                                },

                                function (err) {
                                    $scope.status = 'unable to connect to data' + err;
                                }
                            );

                            $scope.reloadRoute = function () {
                                $location.path('/#');
                                //$window.location.reload()
                            }//end of reload route fnction
                        }
                    }//end of function check_login 
                }
            ]
        );//end of login controller

这是登录的记录集代码:

function getLoginRecord($sql, $elementName = "ResultSet", $params = null) {
    $stmt     = parent::getRecordSet($sql, $params);
    $recordSet = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $nRecords = count($recordSet);
    $loginSuccess = "";
    if ($nRecords == 0) {
        $status = 'error';
        $message = json_encode("Invalid username or password");
        $result = '[]';
        $loginSuccess="{\"status\": \"$status\", \"message\":$message}";
    }
    else {
        $status = 'ok';
        $message = json_encode("Success");
        //$userName=json_encode($recordSet[0]['username']);
        $result = json_encode($recordSet);
        // $session = Session::getInstance();
        // $session->setProperty('user', $recordSet[0]['username']);
        // $session->setProperty('user_email', $recordSet[0]['email']);
        $loginSuccess="{\"status\": \"$status\", \"message\":$message }";
    }
   return $loginSuccess;
}

如果有人帮助我,我真的很感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

设置$http的方式存在几个问题。

您在params配置中使用$http,这是用于GET而不是POST。需要为POST设置data

您正在将contentType切换为表单编码,但尝试在您的php中以json格式接收。如果您希望以编码形式发送,则需要进行大量更改

现在让我们发送为json数据,所以尝试切换到:

$http.post(loginUrl, data). // notice the dot to start the chain to success()
        success(function (response) {
            defer.resolve({
                data: response.status, // create data property with value from response
                result: response,
                user: response.username
            });
            console.log(response);
        }). // another dot to chain to error()
        error(function (err) {
            defer.reject(err);
        });

然后在php中,如果你仍然没有得到正确的响应,你需要做一些非常基本的调试,看看数据是否真的被发送。