【问题标题】:How to redirect from view to another view如何从视图重定向到另一个视图
【发布时间】:2017-10-30 17:16:43
【问题描述】:

我尝试使用 angualrJS 和 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');
        });
    }

});

登录.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';我把它作为评论,因为当我放它时,它会将我重定向到管理员视图但是密码不正确。

提前致谢

【问题讨论】:

    标签: php angularjs ionic-framework


    【解决方案1】:

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

    $Location - Documentation

    尝试使用:

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

    举个例子:请参考下面另一篇帖子的回答:

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

    【讨论】:

      【解决方案2】:

      .success 是 $http 服务的一个属性,所以如果数据变量中有一些值,$window.location 最终会被调用。所以为了改进你可以在 $http 服务中使用 if 条件检查传递的用户名和密码以及它将从服务获得的响应,然后在 if 条件下您可以将其重定向到另一个页面。

      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.');
                  }
              })
          }
      });
      

      你可以使用 angularJS 的 $state.go 功能来代替 $window.location。它会将您的页面重定向到将要提及的特定状态,并会在路由文件中查找该状态,并将执行该状态及其 templateUrl 和控制器。

      【讨论】:

      • 在这两种情况下(密码和用户正确以及不正确时)它不会重定向到其他视图,并且我没有收到任何错误消息
      • 我已经更新了我的答案的代码部分这里是链接stackoverflow.com/a/44259143/6859483 请看看它尝试创建服务然后调用该服务并同时传递数据这个代码对我来说非常适合它应该我猜是你。
      • 谢谢你,但不幸的是它不起作用,我总是收到警报消息但是密码和用户名是正确的!
      【解决方案3】:

      在 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则成功,否则出错。

      【讨论】:

      • 是的,当密码正确时我会成功,当密码错误时我会收到错误消息,但在这两种情况下,我被重定向到另一个视图,而不仅仅是它的正确
      • 不行,密码错误需要重定向登录页面,密码正确需要重定向到首页。有时,我们必须刷新页面,如果我们在成功登录后刷新页面,效果最好
      【解决方案4】:

      .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');
           });
      

      【讨论】:

      • 是的,当密码正确时我会成功,当密码错误时我会收到错误消息,但在这两种情况下,我被重定向到另一个视图,而不仅仅是它的正确
      • @SalamSalam:根据您的代码,它不应该是.. 可能在其他一些事件中发生路由(检查subscribe\.on).. 或者您可以创建 plunker\fiddle 来重现?.,同时在您调用submit 的地方发布您的html 代码。
      • @SalamSalam:根据更新的 html,它仍然没有区别 IMO 答案应该仍然有效,请创建 plunker\fiddle 来重现您的问题。
      • 我尝试创建它,但我不知道如何管理其中的数据库,请问我该怎么做!
      • @SalamSalam:见这个例子 $http.post 和 .then fiddle
      【解决方案5】:

      这里是正确测试您的问题的正常工作代码。如果您仍在寻找解决方案

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


      登录.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>
      

      【讨论】:

      • 我仍在寻找解决方案,但是当我尝试实施您的解决方案时,我收到此错误:“未定义的属性'$serviceData',然后在两种情况下将我重定向到管理视图(但是密码错误)
      • 我能看到你更新的代码吗,因为我没有在应用程序的任何地方使用过 $serviceData。
      • 我的意思是你在 php 文件和 js 文件中的 ServiceDate,我收到未定义的错误消息
      • 你是否在 $http.post 方法中更新了本地机器的路径??
      【解决方案6】:

      谢谢大家,这是这个问题的解决方案,由“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');
      
              });
      

      谢谢大家

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-02
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多