AJAX帖子,Codeigniter 4 /php-控制器没有捕获发布的数据

发布于 2025-02-09 09:46:28 字数 1630 浏览 2 评论 0原文

我有以下Ajax JS。我正在尝试使用id向控制器发帖。在以开头的行中,让ID ...将其记录到控制台时,我可以看到ID编号,但控制器无法接收它。另外,在XHR/有效负载中,我看到了[对象对象],它不知道为什么。

$(document).on('click', '.delete-task', function(event){
    // id comes from data-id
    let id = $(event.currentTarget).attr('data-id');
    $.ajax({
        url:  ajaxUrl+'/admin/tasks/delete',
        type: 'POST',
        // what data you passing.
        data: {
            'id' : id
        },
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    });
});

路线是; $路由 - m gt(['get','post'],'/admin/task/delete','admin \ task \ task \ task_controller :: task_delete');

for我的控制器,我有以下。由于这是一篇文章,我希望能够通过使用id使用$ this-> request-> getVar('id'')工作。我总是得到“成功” => '0'msg'=>返回的“无任务删除”。任何指针都赞赏。

```
public function task_delete(){
    $id = $this->request->getVar('id');
    if(empty($id)){
        $response = [
            'success' => 0,
            'msg' => "No Task To Delete",
        ];
        echo json_encode($response);
    } else {
        $task = new TaskModel();
        $task->task_delete($id);
        $response = [
            'success' => 1,
            'msg' => "Task Deleted",
        ];
        echo json_encode($response);
    }
}```

因此,我可以在控制台的有效载荷中看到ID = 103,但在Somereason,但到达控制器。有趣的是,当我log_message('error',$ request-> getMethod());这是一个关键错误,如空白。甚至log_message('错误',$ request-> isajax());对于空白是关键错误。

I have the below ajax js. I am trying to make a POST to my controller with the id . In the line starting with let id ... when I log it to console, I can see the id number but the controller doesn't receive it. Also in XHR/payload I see [object Object] which do not know why.

$(document).on('click', '.delete-task', function(event){
    // id comes from data-id
    let id = $(event.currentTarget).attr('data-id');
    $.ajax({
        url:  ajaxUrl+'/admin/tasks/delete',
        type: 'POST',
        // what data you passing.
        data: {
            'id' : id
        },
        processData: false,
        contentType: false,
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    });
});

Route is; $routes->match(['get', 'post'], '/admin/tasks/delete', 'Admin\Task\Task_Controller::task_delete');

For my controller, I have the below. As it is a post, I expect to be able to get the id by using $this->request->getVar('id') but doesn't work. I always get 'success' => '0 'msg' => "No Task To Delete" returned. Any pointers appreciated.

```
public function task_delete(){
    $id = $this->request->getVar('id');
    if(empty($id)){
        $response = [
            'success' => 0,
            'msg' => "No Task To Delete",
        ];
        echo json_encode($response);
    } else {
        $task = new TaskModel();
        $task->task_delete($id);
        $response = [
            'success' => 1,
            'msg' => "Task Deleted",
        ];
        echo json_encode($response);
    }
}```

So I can see id=103 in the payload in the console but for somereason but reaching the controller. Interesting also is when I log log_message('error', $request->getMethod()); it is a critical error as blank. Even log_message('error', $request->isAjax()); is critical error as blank.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文