CONCLUSION: I put up here to get the view right, I was using onClick to fetch this function, but I was leaving the request out of the function so it was not working.
I have an action in the controller Posts:
public function pega($id = null)
{
$posts = $this->Post->findById($id);
foreach($posts as $pok)
{
$foda = $pok['love'];
}
$this->set('foda', $foda);
$this->set('_serialize', array('foda'));
}
In my layout I try to make a request to get the data of the handle function and place it inside an html tag:
<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:81/cakephp/posts/pega/<?php echo $post['Post']['id'];? >.json";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var out = JSON.parse(xmlhttp.responseText);
function loap (){
var arr = out[0];
document.getElementById("id01").innerHTML = arr;
}
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
Added:
Since this should only occur when a button is clicked:
<button type="button" onClick="loap()"> Checa </button>
The script stayed like this:
<script>
var x = new XMLHttpRequest();
var url = "http://localhost:81/cakephp/posts/pega/
<?php echo $post['Post']['id'];? >.json";
//Eu tenho que criar a variável url porque preciso passar um id através do link...
x.open("GET", url, true);
x.send();
var response = null;
x.onreadystatechange = function() {
if (x.readyState == 4 && x.status == 200) {
response = JSON.parse(x.responseText);
}
function loap(){
document.getElementById("id01").innerHTML = response[0];
}
</script>