if ($this->getReques开发者_C百科t()->isXmlHttpRequest()) {
$this->_helper->layout->disableLayout();
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('view', 'html');
$ajaxContext->initContext();
}
how does this actually work... my ajax get page is local.maker/profile/check
i got ajax to work fine but i dont know what to edit from the above...
$ajaxContext->addActionContext('???', 'html');
ps.. i am requesting a json
I use the following code to use AJAX context helper.
In your controller create a preDispatch method to set up your contexts like this:
public function preDispatch()
{
$this->_helper->ajaxContext()
->addActionContext('index', array('json', 'html'))
->addActionContext('anotheraction', 'json')
->initContext();
}
And then in you action methods use:
public function indexAction()
{
if ($this->_helper->ajaxContext()->getCurrentContext() == 'json') {
// ajax code here
} else {
// non ajax code here
}
}
Also in your ajax request you must use the variable format to set the current context, for example
http://www.mydomain.com/index/format/json
to request a json response.
Note: The context switcher automatically disables the layout and view, any view variable set in the controller will automatically be encoded into a json string and sent.
I hope this helps
Kind regards
Garry
Please go through the example . It has everything what you are looking for .
http://framework.zend.com/manual/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.contextswitch.ajaxcontext
addActionContext( <action>, <format>);
Can request comment/process/format/json or via query variable . See the example .
精彩评论