[英]Ajax is skipping success:function call
I'm doing a login with ajax, html and php.我正在使用 ajax、html 和 php 登录。 I've already debbuged the php, it's ok and it's returning the json variable I need in the ajax call.
我已经调试了 php,没关系,它返回了我在 ajax 调用中需要的 json 变量。 I have this ajax function:
我有这个 ajax function:
$(document).ready(function(){
$('#login').submit(function() {
var username=$("#username").val();
var password=$("#password").val();
$.ajax({
url: 'login.php',
data: {
username: username,
password: password
},
type: 'post',
dataType: 'json',
success:function(response){
alert(response);
if(response.validacion == "ok"){
alert("Bienvenidos"),
localStorage.loginstatus = "true",
window.location.assign("home.php");
}
if(response.validacion == "error"){
window.location.assign("login.html"),
alert("Datos de usuario incorrectos, inténtelo nuevamente");
}
}
});
});
});
This is the login.php code: (I know it's really bad save password in cookies, but this is my solution for now)这是login.php 代码:(我知道在 cookies 中保存密码真的很糟糕,但这是我现在的解决方案)
<?php
session_start();
?>
<?php
include 'conexion.php';
if(isset($_POST['username'] && $_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
$sql = "SELECT * FROM User WHERE username = '$username' OR Email ='$username'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_array(MYSQLI_ASSOC);
$hash = $row['password'];
if (password_verify($password, $hash)) {
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $row['username'];
$_SESSION['start'] = time();
setcookie("COOKIE_INDEFINED_SESSION", TRUE, time()+31622400);
setcookie("COOKIE_DATA_INDEFINED_SESSION[username]", $username, time()+31622400);
setcookie("COOKIE_DATA_INDEFINED_SESSION[password]", $password, time()+31622400);
echo "Sesión abierta indefinidamente.<br/>";
$respuesta["validacion"] = "ok";
$respuesta["id"] = $row["idUser"];
$respuesta["username"] = $row["username"];
}else{
$respuesta["validacion"] = "error";
$respuesta["mensaje"] = "Contraseña incorrecta";
}mysqli_close($connection);
}else{
$respuesta["validacion"] = "error";
$respuesta["mensaje"] = "Usuario incorrecto";
}mysqli_close($connection);
// encoding array to json format
echo json_encode($respuesta);
?>
I did and inspect element, the username and password are ok, login.php is called, but when I continue the inspection from the line 20, this works until the line 25 aprox and skips to the line 44, the success:function(function) is skipped and the "response" variable is undefined but the login.php is returnint this variable ok:我做了并检查了元素,用户名和密码都可以,login.php 被调用,但是当我从第 20 行继续检查时,这一直有效到第 25 行 aprox 并跳到第 44 行,成功:function(function ) 被跳过并且“响应”变量未定义,但 login.php 返回此变量 ok:
What am I doing wrong?我究竟做错了什么? (sorry for my english, I'm spanish speaker)
(对不起我的英语,我说西班牙语)
Ok, I've solved the problem so I'm going to post what I've done for those who have the same problem:好的,我已经解决了这个问题,所以我将发布我为那些有同样问题的人所做的事情:
the function success:function(response) is taking ALL the "echo" from the login.php, so the first "echo" when the login is ok, is the trouble and response become an undefined var. function 成功:功能(响应)正在从登录中获取所有“回声”。php,所以登录正常时的第一个“回声”,是麻烦和响应成为未定义的变量。 When I post with the wrong credentials, I only have one echo (the json_encode) and I had not problem with the ajax.
当我使用错误的凭据发布时,我只有一个回显(json_encode)并且我对 ajax 没有问题。 So the solution will be this, in the php, after the if(password_verify): if (password_verify($password, $hash)) {
所以解决方案是这样的,在 php 中,在 if(password_verify): if (password_verify($password, $hash)) {
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $row['username'];
$_SESSION['start'] = time();
setcookie("COOKIE_INDEFINED_SESSION", TRUE, time()+31622400);
setcookie("COOKIE_DATA_INDEFINED_SESSION[username]", $username, time()+31622400);
setcookie("COOKIE_DATA_INDEFINED_SESSION[password]", $password, time()+31622400);
$respuesta["validacion"] = "ok";
$respuesta["id"] = $row["idUser"];
$respuesta["username"] = $row["username"];
}else{
$respuesta["validacion"] = "error";
$respuesta["mensaje"] = "Contraseña incorrecta";
}
}else{
$respuesta["validacion"] = "error";
$respuesta["mensaje"] = "Usuario incorrecto";
}
mysqli_close($connection);
// encoding array to json format
echo json_encode($respuesta);
?>
In order to get the value from the response, you need to JSON.parse() it first to allow your self to use response.validacion为了从响应中获取值,您需要首先 JSON.parse() 它允许您自己使用 response.validacion
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.