我正在使用 Phalcon 框架,我有一个控制器“发布”,动作“喜欢”:
视图/布局/post.volt
{% 用于帖子中的帖子 %}
<h1> {{ post.content }} <⁄h1>
<h2> {{ post.like }} <⁄h2>
<a href="#" onclick="like();" data-id="{{ post.id }}" >点赞<⁄a>
{% endfor %}
<!-- -->
<脚本>
var like = function() { var id = /* ... */; // get id of post which raise 'like' event $.ajax({ dataType: 'json', url: 'post/like/' + id }).done(function() { /* Do something meaningful */ });
<⁄脚本>
控制器/PostController.php
/* ... */
公共函数 indexAction() {
$this->view->posts = /* ... */; // read from database to show the view
}
公共函数 likeAction($id) {
// change 'like' of corresponding post with certain 'id' foreach($this->view->posts as post) { if($post->id == $id) $post->like += 1; /* ... */ } // write to database, and re-render entire post view $this->view->posts = /* ... */; // I just assign 'view' again and hope it works :( $result = array('msg' => 'success'); $response = new Response(); return $response->setJsonContent($result);
}
/* ... */
但是点击按钮后,什么都没有改变!我做错什么了吗?任何人都可以帮助我...在此先感谢!
P/S: 对不起我的英语不好...^^