2011-05-27 137 views
0
if ($this->getRequest()->isXmlHttpRequest()) { 
    $this->_helper->layout->disableLayout(); 
    $ajaxContext = $this->_helper->getHelper('AjaxContext'); 
    $ajaxContext->addActionContext('view', 'html'); 
    $ajaxContext->initContext(); 
} 

,这如何实际工作......我的AJAX GET页面local.maker/profile/checkZend公司的GetRequest AJAX

我得到了阿贾克斯的工作很好,但我不知道该怎么从上面的编辑...

$ajaxContext->addActionContext('???', 'html'); 

ps ..我正在请求json

回答

1

我使用下面的代码来使用AJAX上下文助手。

在你的控制器创建一个preDispatch方法来设置你的背景是这样的:

public function preDispatch() 
{ 
    $this->_helper->ajaxContext() 
        ->addActionContext('index', array('json', 'html')) 
        ->addActionContext('anotheraction', 'json') 
        ->initContext(); 
} 

然后在你的操作方法使用:

public function indexAction() 
{ 
    if ($this->_helper->ajaxContext()->getCurrentContext() == 'json') { 
     // ajax code here 
    } else { 
     // non ajax code here 
    } 
} 

而且在你的Ajax请求,你必须使用可变格式来设置当前上下文,例如

http://www.mydomain.com/index/format/json 

要求aj儿子的回应。

注意:上下文切换器会自动禁用布局和视图,控制器中设置的任何视图变量会自动编码成json字符串并发送。

我希望这有助于

亲切的问候

加里