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

匿名5671

目前,我正在尝试使用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

这是登录的recordSet代码:

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;
}

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

查理

该方法$http设置了几个问题

params$http用于GET而不是POST的配置中使用需要设置data为POST

您正在将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中,如果您仍然没有得到正确的响应,则需要进行一些非常基本的调试,以查看是否实际发送了数据。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

登录代码在PHP中不起作用

来自分类Dev

睡眠功能在PHP中不起作用

来自分类Dev

简单的日期功能在php中不起作用

来自分类Dev

邮件功能在PHP中不起作用

来自分类Dev

联接功能在PHP中不起作用

来自分类Dev

邮件功能在PHP中不起作用

来自分类Dev

邮件功能在OO PHP中不起作用

来自分类Dev

帖子功能在PHP中不起作用

来自分类Dev

PHP复制功能在Joomla中不起作用

来自分类Dev

简单的日期功能在php中不起作用

来自分类Dev

查询功能在php7中不起作用

来自分类Dev

功能在Swift中不起作用

来自分类Dev

功能在Swift中不起作用

来自分类Dev

为什么telnetlib写功能在我的python代码中不起作用?

来自分类Dev

React Native 的代码建议和自动完成功能在 atom 中不起作用

来自分类Dev

handleSubmit功能在React的功能组件中不起作用

来自分类Dev

选中/取消选中所有功能在AngularJS中不起作用

来自分类Dev

Angularjs 点击功能在 youtube Iframe 视频中不起作用,但部分使用 css

来自分类Dev

PHP邮件功能在单击时不起作用

来自分类Dev

OpenSSL加密功能在PHP 7.4.5上不起作用

来自分类Dev

PHP邮件功能在Hostgator上不起作用

来自分类Dev

PHP 复制功能在 foreach 循环中不起作用

来自分类Dev

AngularJS代码在Chrome中不起作用

来自分类Dev

angularjs在我的代码中不起作用

来自分类Dev

angularjs在我的代码中不起作用

来自分类Dev

标头功能在php中的javascript后不起作用

来自分类Dev

php邮件功能在新服务器中不起作用

来自分类Dev

搜寻功能在Perl中不起作用

来自分类Dev

Python:模拟功能在芹菜任务中不起作用