尽管服务器站点成功,但ajax调用仍然失败

时间:2020-05-19 14:54:00

标签: javascript php jquery ajax sweetalert2

我在进行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,该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."}

0 个答案:

没有答案