如何在Zend Framework 1.11中以JSON格式返回Application_Model

时间:2012-05-11 14:41:16

标签: php zend-framework json zend-view

我在视图中定义了一个名为Student的变量。在我看来,如何将Student对象显示为JSON?

public function previewAction()
{
    // ... 
    $this->view->student = $student;
}

在我的preview.phtml视图中,我有以下内容:

<script>
    // this doesn't return the Application_Model as a JSON object in the html       
    var studentData = <?php echo Zend_Json::encode($this->student); ?>; 
</script>

1 个答案:

答案 0 :(得分:1)

在php标签附近加上引号

<script>
    // this doesn't return the Application_Model as a JSON object in the html       
    var studentData = "<?php echo Zend_Json::encode($this->student); ?>"; 
</script>