How to use “response” from any XMLHTTPREQUEST in CakePHP (2.5)

我的梦境 提交于 2019-12-02 11:45:39

问题


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();

来源:https://stackoverflow.com/questions/27979813/how-to-use-response-from-any-xmlhttprequest-in-cakephp-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!