0

我在使用 ajax 调用时遇到了一些问题。当它在某一方面取得成功时,它在回报方面就失败了。

基本上我有一个 PHP 函数来删除数据库中的一个 ID。ajax 调用调用该函数,PHP 代码删除了表中的该行,但 ajax 调用不断失败。如果我再次尝试删除它(ID 不再存在),ajax 调用显示为成功,并且正确的 json 可以正常工作。

function DeleteRole(e, id, role) {
    Swal.fire({
        title: 'Are you sure?',
        text: "Do you want to delete Role " + role + "?",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes',
        showLoaderOnConfirm: true,
        confirmButtonClass: 'btn btn-primary',
        buttonsStyling: false,
        showCancelButton: true,
        cancelButtonClass: "btn btn-danger ml-1",
        preConfirm: function () {
            $.ajax({
                    url: 'caller.php',
                    data: {
                        dados: JSON.stringify(id),
                        caller: 'DeleteRole',
                        dataType: 'json'
                    },
                    type: 'POST',
                })
                .done(function (response) {
                    console.log("done");
                    console.log(response);
                })
                .fail(function (response) {
                    console.log("fail");
                    console.log(response);
                })
        },
        allowOutsideClick: function () {
            !Swal.isLoading()
        }
    })
};

PHP函数:

function DeleteRole($mysqli){
    header('Content-Type: application/json; charset=UTF-8');
    $response = array();
    $response["status"] = "error";
    $response["message"] = "";
    if(isset($_POST['dados'])){
        $args["id"] = validate($mysqli, $_POST["dados"]);
        if($stmt = $mysqli->prepare("SELECT * FROM roles WHERE id = ? LIMIT 1")){
            $stmt->bind_param("i", $args["id"]);
            $stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);
            ($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);
            if ($stmt_result->num_rows>0) { 
                $row_data = $stmt_result->fetch_assoc();
                $role_name = $row_data['role_name'];
                if(crud_delete($mysqli, 'roles', $args) == true){
                    $response["status"] = "success";
                    $response["message"] = "Role ".$role_name." deleted with success";
                    echo json_encode($response);  
                }else{
                    $response["status"] = "error";
                    $response["message"] = "There was a problem deleting role ".$role_name;
                    echo json_encode($response);                               
                }
            }else{
                $response["message"] = "ID does not exists.";
                echo json_encode($response);                
            }
        }else{
            $response["message"] = "Something wrong happened.";
            echo json_encode($response);
        }
    }else{
        $response["message"] = "No data given to work.";
        echo json_encode($response);
    }
}

所以,当我第一次调用该函数时,确实删除了一个特定的 ID,它会从数据库中删除并且应该输出:

                $response["status"] = "success";
                $response["message"] = "Role ".$role_name." deleted with success";
                echo json_encode($response);  

取而代之的是,它使我的 ajax 调用的一部分失败。有什么线索吗?

编辑:第一次删除,我有这样的答案,并出现在 ajax 调用的 .fail 中:

Array
(
    [0] => id
)
{"status":"success","message":"Role Administrator deleted with success"}

如果我第二次这样做(第一次已经删除了 ID),我会得到以下响应(并转到 ajax 请求的 .done):

{"status":"error","message":"ID does not exists."}
4

0 回答 0