preventdefault()无法正常工作

lnxxn5zx  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(319)

今天我在测试一个应用程序,我的preventdefault()有问题,在我的计算机上它工作正常,在大多数用户中他们没有任何问题,在测试它的30个用户中,2告诉我相同的错误,他们将他们发送到登录表单的post文件,它实际上正确地将它们登录,因为在json类型的数组中,我确定密码\u verify是否正确,以便与ajax通信,但它直接将它们重定向到insert\u user.php文件,并且不注意preentdefault(),也不注意提交ajax警报和重定向到应该将它们转移到的位置。
这是承载post请求的文件

if (isset($_POST['login-value'])) {
    //die(json_encode($_POST));
    $correo = $_POST['login-email'];

    try {
        include_once 'funciones.php';
        $password = $_POST['login-pass'];
        $stmt = $conn->prepare("SELECT * FROM usuarios WHERE correo_usuario = ?");
        $stmt->bind_param("s", $correo);
        $stmt->execute();
        $stmt->bind_result($id_user, $nombre_user, $apellido_user, $correo_user, $password_user, $tipo, $status);
        if ($stmt->affected_rows) {
            $existe = $stmt->fetch();
            if ($existe) {
                if (password_verify($password, $password_user)) {
                    if ($status == 'activo') {
                        session_start();
                        $_SESSION['usuario'] = $correo_user;
                        $_SESSION['nombre'] = $nombre_user;
                        $_SESSION['id'] = $id_user;
                        $_SESSION['tipo'] = $tipo;
                        $respuesta = array(
                            'respuesta' => 'exito',
                            'usuario' => $nombre_user
                        );
                    } else {
                        $respuesta = array(
                            'respuesta' => 'error'
                        );
                    }
                } else {
                    $respuesta = array(
                        'respuesta' => 'error'
                    );
                }
            } else {
                $respuesta = array(
                    'respuesta' => 'error'
                );
            }
        }
        $stmt->close();
        $conn->close();
    } catch (Exception $th) {
        echo "error: " . $th->getMessage();
    }
    die(json_encode($respuesta));
}

这是带有ajax和preventdefault()的js文件

if (document.getElementById("login-user-form")) {
    document.getElementById("login-user-form").addEventListener('submit', function (e) {
        e.preventDefault();
        var datos = $(this).serializeArray();
        var correo = document.getElementById("login-email").value;
        var password = document.getElementById("login-pass").value;

        //Validaciones
        if (correo == "") {
            Swal.fire(
                'Error!',
                'Ingresa un correo válido',
                'error'
            )
        } else {
            if (password == "") {
                Swal.fire(
                    'Error!',
                    'Ingresa una contraseña válida',
                    'error'
                )
            } else {
                $.ajax({
                    type: $(this).attr('method'),
                    data: datos,
                    url: $(this).attr('action'),
                    dataType: 'json',
                    success: function (data) {
                        var resultado = data; 
                        if (resultado.respuesta =="exito") {
                            Swal.fire({
                                title: 'Correcto',
                                text: "Inicio de sesión correcto!",
                                icon: 'success',
                                showConfirmButton: false
                            })
                            setTimeout(function () {
                                window.location.href = 'index-app';
                            },1000);
                        } else {
                            Swal.fire(
                                'Error!',
                                'Usuario o contraseña incorrecto, intentalo de nuevo!',
                                'error'
                            )
                        }
                    }
                })
            }
        }
    });
}

让我有点困惑的是,为什么在94%的用户中我没有问题,而在这两个用户中却有问题。
唯一不变的是,这两个用户不在谷歌chrome浏览器中工作,我不知道版本,因为他们是普通用户,没有任何类型的程序员对其进行测试。

qco9c6ql

qco9c6ql1#

您可以尝试:删除“e.preventdefault();”在这个脚本中

相关问题