Zend Framework教程-Zend_Helpers-动作助手-Json

Json的使用较简单,以下是文档给出的用法:


Json 用来解码和发送 JSON 响应;


当处理期望数据表响应的 AJAX 请求,JSON 响应迅速变成选择的响应。
JSON 可以立即在客户端被解析,从而快速执行。 




JSON 动作助手完成以下任务: 


    如果布局是打开(enabled)的,则关闭(disable)它。 


    如果视图解析器(ViewRenderer)是打开的,则关闭它。 


    设置 'Content-Type' 响应头为 'application/json'。 


    缺省地,不需要等待动作执行完成,立即返回响应。 


用法很简单:或者把它作为助手代理的方法来调用,或者调用 encodeJson() 和 sendJson() 方法的其中之一: 


class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // do some processing...
        // Send the JSON response:
        $this->_helper->json($data);


        // or...
        $this->_helper->json->sendJson($data);


        // or retrieve the json:
        $json = $this->_helper->json->encodeJson($data);
    }
}




    Note: 保持布局 (Keeping Layouts)
如果你为 JSON 响应有分离的布局 - 也许把 JSON 封装到一些上下文 - 在 JSON 助手的每个方法接受第二个可选的参数:打开或关闭布局的 flag ,传递一个布尔 true 值将使布局保持打开: 




class FooController extends Zend_Controller_Action
{
    public function barAction()
    {
        // Retrieve the json, keeping layouts:
        $json = $this->_helper->json->encodeJson($data, true);
    }
}


你可能感兴趣的:(json,Ajax,function,Class,action,Zend)