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.