How to use the "response" of an XMLHTTPREQUEST in CakePHP (2.5)

1

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>
    
asked by anonymous 16.01.2015 / 09:19

1 answer

2

I'll give you a small example of how I used to use

Cakephp Controller

class JsonController extends AppController
{


    public function teste()
    {
        $this->autoRender = false;

        $json = json_encode(array('nome' => 'StackOverflow'));

        $this->response->type('application/json');
        $this->response->body($json); // json retornado {"nome" : "StackOverflow"}          
    }


}

Javascript:

var x = new XMLHttpRequest
x.open('GET', 'json/teste', true)
x.send();

var response = null;

x.onreadystatechange = function() {
if (x.readyState == 4) {
   response = JSON.parse(x.responseText)
}

}

Update

Based on your code, I can imagine that you want the following:

 public function pega($id = null)
 {                      

    $posts = $this->Post->findById($id);

    $this->response->type('application/json');

    return $this->response->body(json_encode($posts));

}

No HTML

    var x = new XMLHttpRequest
    x.open('GET', 'json/teste', true)
    x.send();

    var response = null;

   var divQuerQueroImprimirOsValores = document.getElementById('ondeVouImprimir')

    x.onreadystatechange = function() {
    if (x.readyState == 4) {
       response = JSON.parse(x.responseText)

       for (key in response) {

           divQuerQueroImprimirOsValores.innerHTML += response[key]    

       }
    }
    
16.01.2015 / 14:42