我的问题是我总是得到“失败”状态,即使我在登录时输入了正确的值。但是,当我将login.php代码中的$sql
修改为数据库中的正确值时,我会获得“成功”状态。我认为将数据从AJAX发送到PHP可能有问题。你能帮我解决一下吗?
这是login.php代码:
header('Access-Control-Allow-Origin: *');
include "database.php";
header("Content-Type: application/json");
$numer=$_POST['number'];
$pass=$_POST['pass'];
$sql="SELECT * FROM `Users` WHERE Phone = '$numer' AND Password = '$pass'";
if ($result=mysqli_query($db,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = $result->fetch_assoc()) {
$dane = array("status" => "success",
"ID" => $row["ID"],
"Name"=> $row["Name"],
"Surname"=> $row["Surname"],
"Phone"=> $row["Phone"],
"Password"=> $row["Password"],
"Admin"=> $row["Admin"],
"Class"=> $row["Class"],
"Banned"=> $row["Banned"]
);
}
echo json_encode($dane);
}
else
{
$dane = array("status"=>"failed");
echo json_encode($dane);
}
mysqli_free_result($result);
}
mysqli_close($db);
?>
这是AJAX代码:
$("#login").click(function(){
var number=$("#number").val();
var pass=$("#password").val();
if($.trim(number).length>0 & $.trim(pass).length>0)
{
$.ajax({
type: "POST",
url: "http://smietana.mechanikrac.website.pl/cgi-bin/janeta/login.php",
data: { number:number, pass:pass},
contentType:"application/json; charset=utf-8",
dataType:'json',
crossDomain: true,
cache: false,
beforeSend: function(){ $("#login").html('Connecting...');
alert(number+pass);},
success: function(data){
alert('test'+data["status"]);
if(data["status"]=="success")
{
localStorage.login="true";
localStorage.number=number;
alert(data["status"]);
window.location.href = "order.html";
$('#witaj').html(number);
}
else if(data["status"]=="failed")
{
alert("Błędne Dane");
$("#login").html('Login again');
}
}
});
}return false;
});
答案 0 :(得分:0)
将数据更改为
data: { 'number':number, 'pass':pass}
然后移除contentType
和dataType