Ajax直接进入错误部分

时间:2015-02-04 07:04:21

标签: java php html ajax

我不明白我的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);">
.....

和我的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);               
            }      
         ......

我有什么问题吗?

2 个答案:

答案 0 :(得分:1)

我想,javascript控制台可能会告诉你更多。或者您可以查看错误状态:

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

答案 1 :(得分:0)

首先,删除php脚本中的echo "33..";

其次:不要将密码直接存储在您的数据库中!