简体   繁体   English

Ajax直接进入错误部分

[英]Ajax going straight to error part

i dont understand my ajax goes straight to the error part, herewith my code: please see my comments on the lines im not sure about 我不明白我的Ajax会直接进入错误部分,并附上我的代码:请在不确定的地方查看我的评论

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script>      
 function validateLogIn(login)
 {
    var username  = $("#username").val();
    var password  = $("#password").val();
    var login  = $("#login").val();
    var remember  = $("#remember").val();
    console.log(username, password, login, remember);
    $.ajax({     
     url: 'php/login.php',                        
     data: {username:username,password:password,login:login,remember:remember},
     type: 'POST',    
     dataType: 'json',      //is this correct??               
     success: function(data)          
    {
      console.log("sdfsdfs   " + data);
      if(data == true){  //is this correct if I json_encode(true) in login.php?
        console.log("sdfsdfs   " + data);
        form.submit();
      } else
      {
        alert('Please log in again, credentials did not match');
      }
    },
   error: function(data) 
   { 
     alert('Please log in again, credentials did not match...' );
     console.log(data);
   } 
 });  
 return false;
}
</script>   
</head>
<body>
   <form action="crud.html" method="post" name="form" onsubmit="return validateLogIn(this);">
.....

and my login.php code: 和我的login.php代码:

 <?php
 $username = $_POST['username'];
 $password = $_POST['password']; 
 $login = $_POST['login'];

 if ($_POST['login'] == 'login') //check if the submit button is pressed
 {
   $remember = $_POST['remember'];  
   if ($username&&$password) //check if the field username and password have values
   {
     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = '';

     $connect=mysqli_connect($dbhost,$dbuser,$dbpass) or die("Unable to Connect");
     echo "11..";
     mysqli_select_db($connect,"clients") or die("Could not open the db");
     echo "22..";
     $sql = "SELECT * FROM clients.users WHERE username='$username'";
     $login = mysqli_query($connect, $sql);
     echo "33..";
     if (mysqli_num_rows($login))
     {
        while ($row = mysqli_fetch_assoc($login)) 
        {
            $db_password = $row['password']; 
            if ($password==$db_password)
            {   
                $loginok = TRUE;
                echo json_encode(true);  //is this correct??
            } else {
              echo json_encode(false);               
            }      
         ......

what have i got wrong please? 请问我怎么了?

The javascript console would probably tell you more, I imagine. 我想,javascript控制台可能会告诉您更多信息。 Or you could look at the error status: 或者您可以查看错误状态:

error: function(data, status, errorString)
{ 
    console.log(data, status, errorString);
}

First, remove echo "33.."; 首先,删除echo "33.."; in your php script. 在您的PHP脚本中。

Secondly: do not store passwords directly in your DB! 其次:不要将密码直接存储在数据库中!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM