与Zendframework的Ajax

时间:2010-02-24 08:32:18

标签: ajax zend-framework

我如何将Ajax与zendframework集成。我需要一些很好的教程。

2 个答案:

答案 0 :(得分:3)

将其放入您的控制器

public function init()
{
    $contextSwitch = $this->_helper->getHelper('contextSwitch');
    $contextSwitch->addActionContext('test', 'json')
                  ->initContext();
}

创建测试操作

public function testAction()
{
    $this->view->var1 = "I'm testing";
}

然后使用jquery在您的视图中执行请求

<script type="text/javascript">

$(document).ready(function(){
    $('#display').ajaxStart(function(){
        $(this).html('Loading...');   
    });

    str = $('#test').val();
    $('#link').bind('click',function(event){
        event.preventDefault();
        $.post(
        this.href,
        { var1: str, format: 'json' }, 
        function(data){
            $('#display').html(data.var1);
        },
        "json"
        );          

    });

});

</script>
<input type="text" name="test" id="test" value="just testing" />
<p>
    <a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'test')); ?>" id="link">Testar</a>
</p>

<div id="display">...</div>

答案 1 :(得分:1)

你可以在你的控制器中使用这样的东西:

    public function testAction() {
        // Remove all of your HTML stuff
        $this->_helper->layout->disableLayout();
        // PHP to create an array form database or something
        // This will return the info as json. It takes care of the header and everything else!
        return $this->_helper->json->sendJson( array('test') );
    }
相关问题