How to use “response” from any XMLHTTPREQUEST in C

2019-03-01 00:43发布

问题:

I got the action "pega" in 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 do a requestto catch the data from function "pega" and put inside tag html:

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:81/booklandia/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();