Welcome Guest, Not a member yet? Register   Sign In
AJAX / Post / delete record not working
#13

(This post was last modified: 06-25-2022, 12:59 AM by spreaderman.)

It seems I was posting a JSON object.  Almost there but not.

Here is the revised javascript:

Code:
$(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',
        data: {
            "id" : id
        },
        headers: {
            "Content-Type": "application/json",
            "X-Requested-With": "XMLHttpRequest"
        },
        processData: true,
        contentType: false,
        dataType: 'json', // expected dataType back from the server
        success: function(data) {
            console.log(data);
            alert("hi: "+JSON.stringify(data));
        }
    });
});

I can get the raw JSON stream with the new code but cannot figure out how to access id;


PHP Code:
public function task_delete(){
 
  log_message('error'print_r($this->request->getRawInput(), TRUE));
 
  
   $data 
$this->request->getJsonVar('id');
 
  log_message('error'print_r($datatrue));
 
  
     $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);
 
    }
 } 

The $this->request->getRawInput() produces;

Code:
ERROR - 2022-06-25 07:43:52 --> Array
(
    [id] => 100
)

But how to access id pls?

According to the documentation, if the CONTENT-TYPE is set to application/json, I should be able to access like this;  $request->getVar('id'); but it is blank.

Code:
            [Content-Type] => CodeIgniter\HTTP\Header Object
                (
                    [name:protected] => Content-Type
                    [value:protected] => application/json
                )
Reply


Messages In This Thread
RE: AJAX / Post / delete record not working - by spreaderman - 06-24-2022, 11:50 PM



Theme © iAndrew 2016 - Forum software by © MyBB