I'm going crazy with the AJAX response I want to pass a variable by ajax and retrieve it with cakephp.
The variable to recover is " option "
jQuery + AJAX:
$(document).on("click", "#modificar-foto-cuenta", function() {
var opcion = $(this).attr("id").valueOf();
$.ajax({
type: "POST",
url: "/Users/home/",
data: "opcion = " + opcion,
dataType: 'html',
success: function (response) { console.log(response); },
error: function () { alert('error'); }
});
$('.modalHome').show();
});
Driver:
public function home() {
if($this->request->is('post')) {
$data = $this->request->getData();
$this->set([
'data' => $data,
'_serialize' => 'data'
]);
}
Vista:
if(isset($data->opcion)) {
$opcion = $data->opcion;
}
Well, there is no way he says he does not recognize data and he is so pancho.
I know that the problem is in the controller, because the ajax returns success, so Ajax processes the post well, but for some reason that I do not know the controller does not detect the post, I tried to change post to ajax with the same result.
What am I doing wrong?
EDIT:
I made some changes:
jQuery + AJAX:
$(document).on("click", "#modificar-foto-cuenta", function() {
var opcion = $(this).attr("id").valueOf();
$.ajax({
type: "POST",
url: "/Users/home/",
data: { opcion: opcion },
dataType: 'html',
success: function (data) { console.log(data); },
error: function (data) { console.log(data); alert('error no ajax'); }
});
$('.modalHome').show();
});
Driver:
public function home() {
if($this->request->is('post')) {
$data = $this->request->getData();
$this->set('data', $data);
}
}
Vista:
<div class="modalHome">
<?php
$paso = $data['opcion'];
echo $this->element('modalHome', ['opcion' => $paso ]);
?>
modalHome
<?php
echo $opcion;
?>
I get the following error:
Notice (8): Undefined variable: data [APP / Template / Users / home.ctp, line 50]
Line 50 is --- > $paso = $data['opcion'];
But the strangest thing of all is that the console.log (data); if you return the value to me:
<div class="modalHome">
modificar-foto-cuenta</div>