How to download views via Ajax using the Zend Framework?

Is it possible to load views using ajax in the zend structure, so the layout page is not updated, but only the main content of the div?

+4
source share
2 answers

use Ajax context switching, you can do this by adding this to your init function in your controller.

public function init() { $ajaxContext = $this->_helper->getHelper('AjaxContext'); $ajaxContext->addActionContext('my', 'html') //my is your action ->initContext(); } 

The html parameter is an Ajax request type. it can be json or xml

 public function myAction() { // get what you are sending to your view : data $this->view->data = $data; } 

create a view my.ajax.phtml to which myAction will try to execute it by default and then include my.ajax.phtml in your view (your main content div)

+5
source

With Zend 1.12, we used Zend_Controller_Action_Helper_Json;

Controller:

 use Zend_Controller_Action_Helper_Json; class MyController extends Zend_Controller_Action { public function init() { Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_Json()); } public function fooAction() { $this->getResponse()->setHttpResponseCode(200); $this->_helper->json(array('value' => 1)); } } 

View:

  • No view file

Output:

 {"value":1} 

Call:

 http://example/my/foo 
0
source

Source: https://habr.com/ru/post/1394951/


All Articles