Javascript 无法将数据从控制器返回到ajax
我正在使用phalcon框架,在我的博客中,我想用ajax添加注释,但问题在于我的控制器查询。我不明白如何用ajax返回查询数据以供查看。我当前的查询脚本返回整个html页面,但我只需要注释数据。谁来帮忙 [控制器] [Jquery] [查看] 输出如下图所示:Javascript 无法将数据从控制器返回到ajax,javascript,php,jquery,ajax,phalcon,Javascript,Php,Jquery,Ajax,Phalcon,我正在使用phalcon框架,在我的博客中,我想用ajax添加注释,但问题在于我的控制器查询。我不明白如何用ajax返回查询数据以供查看。我当前的查询脚本返回整个html页面,但我只需要注释数据。谁来帮忙 [控制器] [Jquery] [查看] 输出如下图所示: 您只能获取注释的文本,并将其作为json文件进行回显。研究一下如何从服务器返回json。互联网上有很多关于如何将php数组解析为json字符串并为ajax请求检索该字符串的参考资料 此外,您还必须更改javascript的ajax请求,
您只能获取注释的文本,并将其作为json文件进行回显。研究一下如何从服务器返回json。互联网上有很多关于如何将php数组解析为json字符串并为ajax请求检索该字符串的参考资料
此外,您还必须更改javascript的ajax请求,以便获取json字符串并将其解析为javascript数组。尝试将这一行移到操作的顶部:
$this->view->disabled();
从您当前代码的外观来看,它永远不会被解雇
也可以考虑重写这个块:
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]);
}
也许可以简单地说:
if(!$comments->create()){
return $this->response->setJsonContent(['status' => 'error']);
) else {
return $this->response->setJsonContent([
'status' => 'success',
'payload' => [
'bcoment' => $comments->bcoment,
'cauthor' => $comments->cauthor,
'entry_id' => $comments->entry_id
]
]);
}
试着这样做:
$('#comentSave').on('click', function(e) {
e.preventDefault();
var coment = $('.comnt').val();
var cid = $('.pid').val();
$.ajax({
type:'POST',
url:'blog/comments',
data:{'bcoment':coment,'postId':cid},
beforeSend: function(){$('.loader').html('Processing...');}
}).done(function(resp){
console.log(typeof resp);
resp = JSON.parse(resp);
resp.forEach(function(value){
if(value.e === 'e1'){alert('Insertion Failed!');}
if(value.e === 'e2'){alert('You are not authorized!');}
if(value.e === 'e3'){if(value.uname === value.cauthor){var cdata = 'bcomBase';}else{cdata = 'bcom';}
$('#datax').append('<div class="'+cdata+'"><img src="uploads/users/'+value.userimg+'"><b>'+value.cauthor+'</b><br/>'+value.bcoment+'</div>');
$('.comentcount').text(value.count);
}
});
$('#blogComment')[0].reset() || $('.comnt').val('');
}).fail(function(){ alert('Reload & try again..!'); }).always(function(){});
return false;
});
public function commentsAction()
{
$this->view->disable();
if(!empty($this->session->get('uname')))
{
$comments = new Comments();
$comments->cauthor = $this->session->get('uname');
$comments->bcoment = $this->request->getPost('bcoment');
$comments->entry_id = $this->request->getPost('postId');
$resData = array();
if($comments->save() === true)
{
$data = Blogs::findFirstByid($this->request->getPost('postId'));
$cc = Comments::countByentry_id($this->request->getPost('postId'));
$user = Users::findFirstByid($this->session->get('id'));
$query = Comments::findByid($comments->id);
foreach($query as $q){$resData[] = array('bcoment' => $q->bcoment, 'cauthor' => $q->cauthor, 'entry_id' => $q->entry_id, 'count' => $cc, 'userimg' => $user->image, 'e' => 'e3', 'uname' => $data->blog_author);}
echo(json_encode($resData));
}
else{$resData[] = array('e' => 'e1');echo(json_encode($resData));}
}
else{$resData[] = array('e' => 'e2');echo(json_encode($resData));}
}
你能举个例子吗?我明白你在做什么。不要在这里重定向:返回$this->response->redirect$this->request->getHTTPReferer;你只需要显示数据。能否显示$comments->create方法?在php脚本中,必须返回包含数据的json字符串。我会找到一个例子,如果我喜欢的话:返回json_encode$comments;然后像下面这样输出:[对象对象]刷新孔页面后立即显示真实数据,如下所示:您执行mysql查询。。。然后你会看到这样一个数组,我想:$row=username=>John,content=>这是我的评论。justdo:header'Content-type:application/json';echo json_encode$行;你能用当前的HTML标记更新你的问题吗?以及所需的结果,请检查成功回调中返回的数据是什么。不知道这里到底发生了什么。您确定此回调中的数据是对象吗?
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]);
}
if(!$comments->create()){
return $this->response->setJsonContent(['status' => 'error']);
) else {
return $this->response->setJsonContent([
'status' => 'success',
'payload' => [
'bcoment' => $comments->bcoment,
'cauthor' => $comments->cauthor,
'entry_id' => $comments->entry_id
]
]);
}
$('#comentSave').on('click', function(e) {
e.preventDefault();
var coment = $('.comnt').val();
var cid = $('.pid').val();
$.ajax({
type:'POST',
url:'blog/comments',
data:{'bcoment':coment,'postId':cid},
beforeSend: function(){$('.loader').html('Processing...');}
}).done(function(resp){
console.log(typeof resp);
resp = JSON.parse(resp);
resp.forEach(function(value){
if(value.e === 'e1'){alert('Insertion Failed!');}
if(value.e === 'e2'){alert('You are not authorized!');}
if(value.e === 'e3'){if(value.uname === value.cauthor){var cdata = 'bcomBase';}else{cdata = 'bcom';}
$('#datax').append('<div class="'+cdata+'"><img src="uploads/users/'+value.userimg+'"><b>'+value.cauthor+'</b><br/>'+value.bcoment+'</div>');
$('.comentcount').text(value.count);
}
});
$('#blogComment')[0].reset() || $('.comnt').val('');
}).fail(function(){ alert('Reload & try again..!'); }).always(function(){});
return false;
});
public function commentsAction()
{
$this->view->disable();
if(!empty($this->session->get('uname')))
{
$comments = new Comments();
$comments->cauthor = $this->session->get('uname');
$comments->bcoment = $this->request->getPost('bcoment');
$comments->entry_id = $this->request->getPost('postId');
$resData = array();
if($comments->save() === true)
{
$data = Blogs::findFirstByid($this->request->getPost('postId'));
$cc = Comments::countByentry_id($this->request->getPost('postId'));
$user = Users::findFirstByid($this->session->get('id'));
$query = Comments::findByid($comments->id);
foreach($query as $q){$resData[] = array('bcoment' => $q->bcoment, 'cauthor' => $q->cauthor, 'entry_id' => $q->entry_id, 'count' => $cc, 'userimg' => $user->image, 'e' => 'e3', 'uname' => $data->blog_author);}
echo(json_encode($resData));
}
else{$resData[] = array('e' => 'e1');echo(json_encode($resData));}
}
else{$resData[] = array('e' => 'e2');echo(json_encode($resData));}
}