Ajax没有触发成功功能

时间:2017-10-01 15:09:13

标签: javascript php jquery json ajax

我正在尝试通过ajax在json中发送身份验证响应。 在chrome devtools中,我得到文件 register.ajax.php 的响应  在“网络”上但从未在javascript文件中接受过。

在user.class.php中,我检查用户ID /用户名或电子邮件是否已经存在,如果有,则会将状态发送为“错误”或“成功”;

user.class.php

<%- comment.author.name %>

通过ajax文件,我只需在设置动作'register_client'时激活register_client公共函数(似乎也很好);

register.ajax.php

   public function register_client($uid, $email, $pwd)
  {
  $this->db = DB::getInstance();
  $stmt = $this->db->prepare("SELECT * FROM user_accounts WHERE uid = :uid OR email = :email");
  $stmt->bindParam(":uid", $uid);
  $stmt->bindParam(":email", $email);
  $stmt->execute();
  $query = $stmt->fetch(PDO::FETCH_ASSOC);
  $resultado_uid = $query['uid'];
  $resultado_email = $query['email'];
  if ($resultado_uid || $resultado_email)
     {
        $response_array['status'] = 'error';
        echo json_encode($response_array);



     }
    else
     {
        $response_array['status'] = 'success';
        echo json_encode($response_array);




     $basic_password = $pwd;
     $hashed_password = password_hash($basic_password, PASSWORD_BCRYPT);
     $stmt = $this->db->prepare("INSERT INTO user_accounts(uid, email, pwd) VALUES (:uid, :email, :pwd)");
     $stmt->bindParam(":uid", $uid);
     $stmt->bindParam(":email", $email);
     $stmt->bindParam(":pwd", $hashed_password);
     $stmt->execute();


     }
  }

注册表格应该没问题(这只是一个简单的形式,动作也匹配)

register.php

<?php
require_once "../header.php";
  if(isset($_POST['action'])) $action = $_POST['action'];

    if($action == 'register_client'){

    $cliente->register_client($_POST['uid'], $_POST['email'], $_POST['pwd']);

    }

这里似乎是问题所在,成功函数不会检索任何数据。试图在其中执行console.log(数据)以查看它给了我什么,到目前为止没有。

      <?php
require_once "header.php";
require_once "nav.php"
?>


<form action="" method="POST">


<input type="text" name="uid" id="uid" placeholder="username"><br>

<input type="email" name ="email" id="email" placeholder="email"><br>

<input type="password" name="pwd" id="pwd" placeholder="password"><br>

<button name="register_client" id="register_client" type="submit">Register</button>

</form>


<?php 

    include "footer.php";

?>

1 个答案:

答案 0 :(得分:0)

register.ajax.php你没有回应任何事情。如果您想获得输出,则必须在json

中回显stringregister.ajax.php

register.ajax.php中,如果您添加echo "success";,那么您将获得成功的控制台日志。

如果您想返回array,则可以使用echo json_encode($yourArrayData);

相关问题