preventdefault()无法正常工作

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

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

  1. if (isset($_POST['login-value'])) {
  2. //die(json_encode($_POST));
  3. $correo = $_POST['login-email'];
  4. try {
  5. include_once 'funciones.php';
  6. $password = $_POST['login-pass'];
  7. $stmt = $conn->prepare("SELECT * FROM usuarios WHERE correo_usuario = ?");
  8. $stmt->bind_param("s", $correo);
  9. $stmt->execute();
  10. $stmt->bind_result($id_user, $nombre_user, $apellido_user, $correo_user, $password_user, $tipo, $status);
  11. if ($stmt->affected_rows) {
  12. $existe = $stmt->fetch();
  13. if ($existe) {
  14. if (password_verify($password, $password_user)) {
  15. if ($status == 'activo') {
  16. session_start();
  17. $_SESSION['usuario'] = $correo_user;
  18. $_SESSION['nombre'] = $nombre_user;
  19. $_SESSION['id'] = $id_user;
  20. $_SESSION['tipo'] = $tipo;
  21. $respuesta = array(
  22. 'respuesta' => 'exito',
  23. 'usuario' => $nombre_user
  24. );
  25. } else {
  26. $respuesta = array(
  27. 'respuesta' => 'error'
  28. );
  29. }
  30. } else {
  31. $respuesta = array(
  32. 'respuesta' => 'error'
  33. );
  34. }
  35. } else {
  36. $respuesta = array(
  37. 'respuesta' => 'error'
  38. );
  39. }
  40. }
  41. $stmt->close();
  42. $conn->close();
  43. } catch (Exception $th) {
  44. echo "error: " . $th->getMessage();
  45. }
  46. die(json_encode($respuesta));
  47. }

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

  1. if (document.getElementById("login-user-form")) {
  2. document.getElementById("login-user-form").addEventListener('submit', function (e) {
  3. e.preventDefault();
  4. var datos = $(this).serializeArray();
  5. var correo = document.getElementById("login-email").value;
  6. var password = document.getElementById("login-pass").value;
  7. //Validaciones
  8. if (correo == "") {
  9. Swal.fire(
  10. 'Error!',
  11. 'Ingresa un correo válido',
  12. 'error'
  13. )
  14. } else {
  15. if (password == "") {
  16. Swal.fire(
  17. 'Error!',
  18. 'Ingresa una contraseña válida',
  19. 'error'
  20. )
  21. } else {
  22. $.ajax({
  23. type: $(this).attr('method'),
  24. data: datos,
  25. url: $(this).attr('action'),
  26. dataType: 'json',
  27. success: function (data) {
  28. var resultado = data;
  29. if (resultado.respuesta =="exito") {
  30. Swal.fire({
  31. title: 'Correcto',
  32. text: "Inicio de sesión correcto!",
  33. icon: 'success',
  34. showConfirmButton: false
  35. })
  36. setTimeout(function () {
  37. window.location.href = 'index-app';
  38. },1000);
  39. } else {
  40. Swal.fire(
  41. 'Error!',
  42. 'Usuario o contraseña incorrecto, intentalo de nuevo!',
  43. 'error'
  44. )
  45. }
  46. }
  47. })
  48. }
  49. }
  50. });
  51. }

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

qco9c6ql

qco9c6ql1#

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

相关问题