gpt4 book ai didi

java - Ajax直接进入错误部分

转载 作者:行者123 更新时间:2023-12-01 11:57:36 24 4
gpt4 key购买 nike

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

请问我错了什么?

最佳答案

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

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

关于java - Ajax直接进入错误部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28315380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com