如何从视图重定向到另一种视图



我尝试使用AngualRJ和PHP进行身份验证。我通过console.log对其进行了测试,当密码不正确时,我会收到错误消息,当密码正确时,我没有得到任何东西,在这种情况下,我想被骑上其他视图,我该怎么做:

app.js

app.controller('loginCtrl', function($scope, $location,$state,$http,$window){
    $scope.submit = function()
    {
        data = {
            'NomClient' : $scope.NomClient,
            'mdp' : $scope.mdp
        };
        $http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config)
        {
          // $window.location.href = '#/admin';
            console.log(data);
        })
        .error(function(data, status, headers, config)
        {
            console.log('error');
        });
    }
});

login.php

 <?php  
$data = json_decode(file_get_contents("php://input"));
 $connect = mysqli_connect("localhost", "root", "", "test");  

 if(count($data) > 0)  
 { 
$NomClient=mysqli_real_escape_string($connect, $data->NomClient);
$mdp=mysqli_real_escape_string($connect, $data->mdp);

$query = 'SELECT * FROM `client` WHERE NomClient = "'.$NomClient.'" AND   mdp= "'.$mdp.'"';
$q = mysqli_query($connect , $query);
if(mysqli_num_rows($q) > 0 )
        { 
            $_SESSION["logged_in"] = true; 
            $_SESSION["naam"] = $NomClient; 
        }
        else
        {
            echo 'The username or password are incorrect!';
        }
}
 ?>

如您所见,我在app.js://$ window.location.href ='#/admin'中有一个注释行';我将其作为评论,因为当我提出时,它将我重定向到管理视图,但是密码不正确。

预先感谢

使用angularjs,您可以使用$location服务

$位置 - 文档

尝试使用:

$location.path("your new path here")

示例:请参阅另一个帖子的以下答案:

https://stackoverflow.com/a/14387747/7018180

在login.php

中尝试此代码
if(mysqli_num_rows($q) > 0 )
  { 
       $_SESSION["logged_in"] = true; 
       $_SESSION["naam"] = $NomClient; 
       $result['code'] = 200;
       $result['message'] ='Logged In';
  }
  else
  {
       $result['code'] = 603;
       $result['message'] ='The username or password are incorrect!';
  }
$resultstring = json_encode($result);
$resultstring = str_replace("null",'""',$resultstring);
echo $resultstring ;
die;

并在js中检查结果代码,如果是200,则将在其他明智的方面取得成功。

.success.error更改为 .then(),更改您的代码:

   $http.post('http://localhost/deb/login.php', data).then(function(response) {          
            console.log(response.data);
            $window.location.href = '#/admin';
          }, function(error) {
            console.log('error');
     });

.success是$ http服务的属性如果$ HTTP服务中的条件将检查已通过的用户名和密码,并以其从服务中获得的响应,然后在条件下您可以将其重定向到另一页。

app.service('AuthenticationService', function ($http, $window){
    this.login = function (username, password) {
        return $http({
            url: 'http://localhost/deb/login.php',
            method: 'POST',
            params: {
                NomClient : username,
                mdp : password
            }
        })
    };
});
app.controller('loginCtrl', function ($scope, $state, $http, $window, AuthenticationService, $remember) {
    $scope.submit = function()
    {
        AuthenticationService.login($scope.NomClient,$scope.mdp)
        .then(function(response) {
            if (response.data.NomClient == $scope.NomClient && response.data.mdp == $scope.mdp) 
            {
                $state.go('application.home');
            }
            else {
                alert('Credentials do not match our records. Please try again.');
            }
        })
    }
});

,而不是$ window。location您可以使用$ state.go angularjs的功能。它将您的页面重定向到要提及的特定状态,并将在路由文件中寻找该状态,并及

这是正确测试的问题的正确处理代码。如果您仍在寻找解决方案

app.js

app.controller('loginCtrl', function ($scope, $http,$state) {
    $scope.submit = function ()
    {
        $scope.data = {
            username: $scope.username,
            password: $scope.password
        }
        $http.post('http://localhost/HTML5Application/public_html/login.php', {serviceData: $scope.data})
                .success(function (data) {
                    alert(data);
                    $state.go('newState');
                })
                .error(function (data) {
                    alert("problem with service call");
                });
    };
});


login.php

    $data = json_decode(file_get_contents("php://input"));
    $connect = mysqli_connect("localhost", "root", "", "embroidery");
    if (count($data) > 0) {
        $username = mysqli_real_escape_string($connect, $data->serviceData->username);
        $password = mysqli_real_escape_string($connect, $data->serviceData->password);
        $query = 'SELECT * FROM `client` WHERE username = "' . $username . '" AND   password= "' . $password . '"';
        $q = mysqli_query($connect, $query);
        if (mysqli_num_rows($q) > 0) {
            $_SESSION["logged_in"] = true;
            $_SESSION["name"] = $username;
            echo $_SESSION["name"];
        } else {
            echo 'The username or password are incorrect!';
        }
    }
    ?>


login.html

<div class="list list-inset" ng-controller="loginCtrl">
    <label class="item item-input">
        <input type="text" placeholder="Username" required="" ng-model="username"> 
    </label> 
    <label class="item item-input">
        <input type="password" placeholder="Password" ng-model="password"> 
    </label> 
    <button class="button button-block button-positive" name="submit" ng-click="submit()">Login</button>       
</div>

谢谢大家,这是这个问题的解决方案,由" mr_perfect"解决:我们只需要添加宗教信仰:

$http.post('http://localhost/deb/login.php', data)
        .success(function(data, status, headers, config,result)
        {
            console.log(data);
            if(data.code == 200){
            $state.go('admin');
          }
        })
        .error(function(data, status, headers, config, rsult)
        {
            console.log('error');
        });

感谢你们

最新更新