Ajax响应错误

I am trying to create a user registration form and get the correct responses from the form validation on the server side (php) with ajax. Currently my success and error response handlers are working correctly but I also want to show error messages to the user if, for some reason, there is a database connect error or validation fails on the client side and an empty field in the form is passed in the post method. I have tried to implement these responses but I receive a response code: 200 on the error handler and not the specified status that I pass in the php code. Please view the comments in my code for what's not working.

JS

let firstname = $("#firstname").val();
  let surname = $("#surname").val();
  let email = $("#email").val();
  let username = $("#usernameSignup").val();
  var password = $("#passwordSignup").val();
  var passwordConfirm = $("#passwordConfirm").val();
  $.ajax({
    type: "post",
    url: "userRegistration.php",
    data: {
      firstname: firstname,
      surname: surname,
      email: email,
      usernameSignup: username,
      passwordSignup: password,
      passwordConfirm: passwordConfirm
    },
    dataType: "json",
    error: function(data) {
      console.log(data.status); // This passes 200 to the console log if there is an empty field and if I remove the dbConn.php file. 
      console.log(data.message);
      console.log("Not Successful Test");
      if (data.status == "error") {
        console.log("Error, Didn't Execute Query"); // This error data.status response works
      } else if (data.status == "empty") {
        console.log("You have an empty field"); // Want to display this to console if a field is empty.
      } else if (data.status == "connectionError") {
        console.log("Didn't connect to database"); // Want to display this to console if unable to connect to database 
      }
    },
    success: function(data) {
      console.log(data.status);
      console.log("successfulTest");
      if (data.status == "success") {
        console.log("Registration was successful"); // This success data.status works 
      }
    }
  });

userRegistration.php

<?php
// try catch statement to connect to database through getConnection() function in dbConn.php file
try {
    require_once("dbConn.php");
    $dbConn = getConnection();

    // Form validation for POST method to check fields are not empty and sets variables for sql query later. 
    if (isset($_POST['firstname']) && !empty($_POST['firstname'])) {
        $firstname = filter_has_var(INPUT_POST, 'firstname') ? $_POST['firstname'] : null;
        $firstname = trim($firstname);
    }
    if (isset($_POST['surname']) && !empty($_POST['surname'])) {
        $surname = filter_has_var(INPUT_POST, 'surname') ? $_POST['surname'] : null;
        $surname = trim($surname);
    }

    if (isset($_POST['email']) && !empty($_POST['email'])) {
        $email = filter_has_var(INPUT_POST, 'email') ? $_POST['email'] : null;
        $email = trim($email);
    }

    if (isset($_POST['usernameSignup']) && !empty($_POST['usernameSignup'])) {
        $usernameSignup = filter_has_var(INPUT_POST, 'usernameSignup') ? $_POST['usernameSignup'] : null;
        $usernameSignup = trim($usernameSignup);
    }

    if (isset($_POST['passwordSignup']) && !empty($_POST['passwordSignup'])) {
        $passwordSignup = filter_has_var(INPUT_POST, 'passwordSignup') ? $_POST['passwordSignup'] : null;
    }

    if (isset($_POST['passwordConfirm']) && !empty($_POST['passwordConfirm'])) {
        $passwordConfirm = filter_has_var(INPUT_POST, 'passwordConfirm') ? $_POST['passwordConfirm'] : null;
    }

    // Checks to see if both passwords entered match, to set the passwordHash variable.
    if ($passwordSignup == $passwordConfirm) {
        $passwordHash = password_hash($passwordSignup, PASSWORD_DEFAULT);
    }

    // If all the previous steps are valid and variables are set, try to run the SQL query to make new account.
    if ($firstname && $surname && $email && $usernameSignup && $passwordHash) {
        try {
            $sqlQuery = "INSERT INTO GH_users (firstname, surname, email, accountConfirmed, username, passwordHash)
        VALUES ('$firstname', '$surname', '$email', 0, '$usernameSignup', '$passwordHash')";
            $execute = $dbConn->exec($sqlQuery);
            $response_array["status"] = "success";
        } catch (PDOException $e) {
            $response_array["status"] = "error";
        }
    } else {
        $response_array["status"] = "empty"; // Should pass "empty" if any of the fields are empty
    }
} catch (Exception $e) {
    $response_array["status"] = "connectionError"; // Should pass "connectionError" if unable to connect to dbConn.php file
}

header("Content-type: application/json");
echo json_encode($response_array);
exit;

I found problems in your JS code. My test result:

enter image description here

The problem is: You put success option inside error option

Fix:

$.ajax({
        type: "post",
        url: "/admin/site/test",
        data: {
             Your data
        },
        dataType: "json",
        error: function(data) {
            console.log(data.status); // This passes 200 to the console log if there is an empty field and if I remove the dbConn.php file.
            console.log(data.message);
            console.log("Not Successful Test");
            if (data.status == "error") {
                console.log("Error, Didn't Execute Query"); // This error data.status response works
            } else if (data.status == "empty") {
                console.log("You have an empty field"); // Want to display this to console if a field is empty.
            } else if (data.status == "connectionError") {
                console.log("Didn't connect to database"); // Want to display this to console if unable to connect to database
            }
        },
        success: function(data) {
            console.log(data.status);
            console.log("successfulTest");
            if (data.status == "success") {
                console.log("Registration was successful"); // This success data.status works
            }
        }
        });