Ajax edit function

How can I edit some model using Ajax and not refreshing the page ?

if ($this->request->is([‘patch’, ‘post’, ‘put’])) {
$option = $this->Options->patchEntity($option, $this->request->data);
if ($this->Options->save($option)) {
//what to do here ?
} else {
//what to do here ?
}
}

Method is working except for that.

if ($this->request->is([‘patch’, ‘post’, ‘put’])) {
$option = $this->Options->patchEntity($option, $this->request->data);
if ($this->Options->save($option)) {
    //success
    $results['result'] = 'success';
} else {
    //fail
    $results['result'] = 'fail';
}
return $this->response
            ->withType('application/json')
            ->withStringBody(json_encode($results));
}

Similar results:


Then, you need use JQuery ajax, call url and handle result of request: show result,etc…

Thank you.
I forgot to mention cake version : 3.3.7
so withType and widthStringBody do not exist.

So I’ve done this :

if ($this->Options->save($option)) {
//success
$results[‘result’] = ‘success’;
} else {
//fail
$results[‘result’] = ‘fail’;
}
$this->response->type(‘json’);
$this->response->body(json_encode($results));
$this->RequestHandler->renderAs($this, ‘ajax’);
return $this->response;

And it does not work !

Works at end !
I had an error in my javascript code.

Thank you again.