简体   繁体   English

AJAX 总是认为 php 返回成功即使失败

[英]AJAX always thinks php returns are successful even after failure

I have a php script that adds a new user account.我有一个添加新用户帐户的 php 脚本。 Part of the addAccount() checks if the username is valid and if not, returns an exception that it's not available (fatal error shows up). addAccount() 的一部分检查用户名是否有效,如果无效,则返回它不可用的异常(出现致命错误)。 My issue is that the AJAX interprets everything as a success and displays the success message no matter what.我的问题是 AJAX 将所有内容都解释为成功并无论如何都显示成功消息。 How do I fix this or at least catch the fatal error and display the proper message?如何解决此问题或至少捕获致命错误并显示正确的消息?

$(document).on('click', '#createUserBtn', function(e){
    e.preventDefault();
    $.ajax({
        url:'addUser.php',
        type:'post',
        data:$('#addUser').serialize(),
        success:function(){

                toastr.success("User successfully added!");
            },
        error: function(){
            toastr.warning('Uh-oh! Something went wrong with adding this user!');
        }
    });
});

addUser.php添加用户.php

<?php
session_start();
/* Include the database connection file (remember to change the connection parameters) */
require './db_inc.php';

/* Include the Account class file */
require './account_class.php';

  $type = $_POST['type'];
  $username = $_POST['uname'];
  $password = $_POST['password'];
  $comp = $_POST['company'];
  $email = $_POST['email'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $query = $pdo->query("SELECT * FROM accounts WHERE email ='".$email."'");

$account = new Account();
// Will print all the values received.
    $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
    header('Location: ./dashboard.php?user='.$username);



?>

Here is the addAccount function that is used...这是使用的 addAccount 函数...

    public function addAccount(string $name, string $passwd, string $comp, string $email, string $fname, string $lname, string $type): int
    {
        /* Global $pdo object */
        global $pdo;

        /* Trim the strings to remove extra spaces */
        $name = trim($name);
        $passwd = trim($passwd);

        /* Check if the user name is valid. If not, throw an exception */
        if (!$this->isNameValid($name))
        {
            throw new Exception('Invalid user name');
        }

        /* Check if the password is valid. If not, throw an exception */
        if (!$this->isPasswdValid($passwd))
        {
            throw new Exception('Invalid password');
        }

        /* Check if an account having the same name already exists. If it does, throw an exception */
        if (!is_null($this->getIdFromName($name)))
        {
            throw new Exception('User name not available');
        }

        /* Finally, add the new account */

        /* Insert query template */
        $query = 'INSERT INTO login.accounts (account_name, account_passwd, fname, lname, company, email, user_type) VALUES (:name, :passwd, :fname, :lname, :comp, :email, :type)';

        /* Password hash */
        $hash = password_hash($passwd, PASSWORD_DEFAULT);

        /* Values array for PDO */
        $values = array(':name' => $name, ':passwd' => $hash, ':lname' => $lname, ':fname' => $fname, ':comp' => $comp, ':email' => $email, ':type' => $type);

        /* Execute the query */

            $res = $pdo->prepare($query);
            $res->execute($values);

        /* Insert query template */





        /* Return the new ID */
        return $pdo->lastInsertId();
    }

You can use http_response_code() to return a HTTP error code (usually code 500 : internal server error).您可以使用http_response_code()返回 HTTP 错误代码(通常代码 500 :内部服务器错误)。

You can do that with a try/catch block :你可以用 try/catch 块做到这一点:

try
{
     ...
     $account = new Account();
     // Will print all the values received.
     $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
     header('Location: ./dashboard.php?user='.$username);
}
catch(Exception $e)
{
    http_response_code(500);
    exit();
}

First you can't redirect user via php if you request something from javascript, remove this line from addUser.php首先,如果您从 javascript 请求某些内容,则无法通过 php 重定向用户,请从 addUser.php 中删除此行

header('Location: ./dashboard.php?user='.$username);

Now to return result from php to client you must DIE php with value, the best way is JSON现在要将结果从 php 返回到客户端,您必须使用值DIE php,最好的方法是 JSON

In addUser.php check what you want and return the value like this:在 addUser.php 检查你想要什么并返回这样的值:

    <?php

session_start();
/* Include the database connection file (remember to change the connection parameters) */
require './db_inc.php';

/* Include the Account class file */
require './account_class.php';

  $type = $_POST['type'];
  $username = $_POST['uname'];
  $password = $_POST['password'];
  $comp = $_POST['company'];
  $email = $_POST['email'];
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $query = $pdo->query("SELECT * FROM accounts WHERE email ='".$email."'");

$account = new Account();
// Will print all the values received.
    $newId = $account->addAccount($username, $password, $comp, $email, $fname, $lname, $type);
        if(intval($newId) > 0) // if user created
            die(json_encode(array('status' => 'ok')));
        else
            die(json_encode(array('status' => 'error')));
    ?>

Then change your clientSide like below:然后像下面这样更改你的客户端:

$(document).on('click', '#createUserBtn', function(e){
    e.preventDefault();
    $.ajax({
        url:'addUser.php',
        type:'post',
        data:$('#addUser').serialize(),
        dataType: "json", // <- Add this line
        success:function(response){ // <- add response parameter
                //Here check result
                if(response['status'] == 'ok')
                    toastr.success("User successfully added!");
                else
                    toastr.warning('Uh-oh! Something went wrong with adding this user!');

            },
        error: function(){

        },
    statusCode: { // <- Add this property
        500: function() {
             toastr.warning('Uh-oh! Something went wrong with adding this user!');
        }
    }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM