【发布时间】:2017-12-23 20:39:34
【问题描述】:
我正在使用 phalcon 框架,在我的博客中,我想用 ajax 附加 cmets,但问题出在我的控制器查询中。我不明白如何返回查询数据以使用 ajax 查看。我当前的查询脚本返回整个洞 html 页面,但我只需要注释数据。有人请帮忙
[控制器]
public function detailsAction($id)
{
$blog = Blogs::findFirstByid($id);
#Comments Retrieve
$coment = Comments::find();
}
public function bCommentAction()
{
if($this->session->has('uname'))
{
if($this->request->isPost() == true || $this->request->isAjax() == true)
{
$comments = new Comments();
$comments->cauthor = trim($this->session->get('uname'));
$comments->bcoment = trim($this->request->getPost('bComent'));
$comments->entry_id = trim($this->request->getPost('postId'));
if(!$comments->create() == true)
{
$this->flashSession->success("SUCCESS:: Comments inserted");
return $this->response->redirect($this->request->getHTTPReferer());
}
else
{
return $this->response->setJsonContent(['bcoment' => $comments->bcoment,
'cauthor' => $comments->cauthor, 'entry_id' => $comments->entry_id]);
}
}
else{echo('Request is Not ajax!');}
}
else
{
echo('<script>alert("You are not loggedin!");</script>');
}
$this->view->disabled();
}
[Jquery]
$('#blogComment').submit(function(e){
e.preventDefault();
var blogComent = $("input[name='bComent']").val();
var postsId = $("input[name='idPost']").val();
$.ajax({
url:'blog/bComment/',
type: 'POST',
cache: false,
data: {'postId':postsId,'bComent':blogComent},
success: function(data){
$('#datax').append('<div class="bcom">'+json.stringify(data)+'</div>').fadeIn(500);
}
});
return false;
});
[查看]
<div id="datax">
{% for coment in comented %}
{% if coment.entry_id === detail.id %}
<div class="bcom">
{% for user in logged %}
{% if coment.cauthor === user.uname %}
{{ image('data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=',"alt": "Some Photo","data-src":"uploads/users/"~user.image,"title":""~user.uname,"class":"comentedId") }}
{% endif %}
{% endfor %}
<b>{{coment.cauthor}}</b><br/>
{{coment.bcoment}}
</div>
{% endif %}
{% endfor %}</div>
输出如下图所示:
【问题讨论】:
-
你能用当前的 HTML 标记更新你的问题吗?想要的结果,好吗?
-
只检查成功回调中返回的数据是什么。不确定这里到底发生了什么。您确定数据是此回调中的对象吗?
标签: javascript php jquery ajax phalcon