您好我有一个Zend Framework应用程序,以下代码是popularAction
控制器
public function popularAction()
{
$type = $this->_getParam('ref',1);
if($type == 'reviews'){
$businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
$result = $businessReviewMapper->getTotalVote();
$rotd = $businessReviewMapper->getROTD($result['review_id']);
$rotd[0]['u_img'] = $this->view->getLoginUserImage($rotd[0]['social_id'],$rotd[0]['login_type'],null,null,square);
$rotd[0]['rating'] = $this->view->getRatingImg($rotd[0]['rating']);
$rotd[0]['business_name_url'] = preg_replace("![^a-z0-9]+!i","-", $rotd[0]['business_name']);
$this->render('reviews');
$this->_helper->json($rotd);
} elseif($type == 'openings') {
$this->view->text = "New Openings";
} else {
$this->_helper->redirector('index', 'index', 'default');
}
}
当用户浏览到http://localhost/business/popular?ref=reviews
时,上述控制器代码将呈现reviews.phtml模板。现在在模板本身内部存在对数据的ajax请求,如下所示:
function getPopular()
{
var count=1;
$.ajax({
url:"<?=$this->baseUrl('business/popular?ref=reviews')?>",
data:{'count':count},
dataType:"json"
type:"POST",
success:function(data){
alert('ok')
}
});
遗憾的是$this->_helper->json($rotd);
没有将数据传递给reviews.phtml,但显示了zend db model返回的json数据,我可能错了?谢谢
答案 0 :(得分:1)
如果您的目标是,要在Ajax请求上发送JSON而不是.phtml文件,请尝试实现此目的:
if ($this->getRequest()->isXmlHttpRequest()) {
if ($this->getRequest()-isPost()) {
$this->_helper->json($rotd);
}
}
这部分检查请求是否,让我们调用它:ajax based ..