如何修复确认删除不工作在codeigniter, AJAX ?

i34xakig  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(104)

我错过了什么?在“你确定你要删除”中的“确定”之后,我什么都没有得到。但是当我试图提醒$id时,它工作了。是 AJAX 的问题吗?
我在我的footer.php里有这个

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>

<script>

    $(document).ready(function () {

        $('.confirm-delete').click(function (e) {
            e.preventDefault();

            confirmDialog = confirm("Are you sure you want to delete?");
            if(confirmDialog)
            {
                var id = $(this).val();
                // alert(id);
                $.ajax({
                    type: "DELETE",
                    url: "/employee/confirmdelete/"+id,
                    success: function (response) {
                        alert("Data deleted successfuly");
                       
                    }
                });
            }

        });
    });

</script>

我在我的routes.php里有这个

$route['employee/confirmdelete/(:any)']['DELETE'] = 'Frontend/EmployeeController/delete/$1';

我的删除按钮

<button type="button" class="btn btn-danger confirm-delete" value="<?= $row->id; ?> ">Confirm Delete</button>

我在控制器中的删除功能

public function delete($id)
{
    $this->load->model('EmployeeModel');
    $this->EmployeeModel->deleteEmployee($id);  
    redirect(base_url('employee'));
}

我模型删除功能

public function deleteEmployee($id)
{
    return $this->db->delete('employee', ['id' => $id]);
}"
juzqafwq

juzqafwq1#

您应该更改此设置

$.ajax({
                    type: "DELETE",
                    url: "/employee/confirmdelete/"+id,
                    success: function (response) {
                        alert("Data deleted successfuly");
                       
                    }
                });

对此

$.ajax({                    
                    url: "/employee/confirmdelete/"+id,
                    type: "POST",
                    dataType: "JSON"
                    success: function (response) {
                        alert("Data deleted successfuly");
                       
                    }
                });

相关问题