Layout stop for custom routing Ajax request

How can I stop the rendering of the layout when sending a request via AJAX .. The problem I am facing is the json data reflected in the browser, and not passed to the jquery functions

here my script

jQuery.ajax({
    url: "/getPrivileges",
    type: "POST",
    dataType: 'JSON',
    success: function(privileges){
        alert('hellooo');

        buildTree(privileges,".privBox h2");
        if($(".privPrivilege"))
            $("#loading").css("visibility","hidden");
    },
    error: function (request, status, error) {
        alert('Error'+request.responseText);
    }

});

and here routing

resources.router.routes.privilege.route = /getPrivileges
resources.router.routes.privilege.defaults.module = privileges
resources.router.routes.privilege.defaults.controller = privilege
resources.router.routes.privilege.defaults.action = get-privileges

and here is my controller

  public function getPrivilegesAction() {

        if ($this->getRequest()->isXmlHttpRequest()) {
         ...........
         ...........
          $this->_helper->json($appPrivArray);
          $this->_helper->viewRenderer->setNoRender(TRUE);
          $this->_helper->layout->disableLayout();
          $response = $this->getResponse();
          $response->setHeader('Content-type', 'application/json', true);
   }


 }

At first, I am faced with the fact that the layout is still displayed, but now json is printed on the screen, even I do not have the get-privileges.phtml access page.

and in the controller's init () method, I do it like

 public function init() {
    $ajaxContextSwitch = Zend_Controller_Action_HelperBroker::getStaticHelper('AjaxContext');
    $ajaxContextSwitch->setDefaultContext('json');
    $ajaxContextSwitch->addActionContext('getPrivileges', 'json');
    $ajaxContextSwitch->initContext();
}

how can i answer by responding to jquery callback function! It is important to note that not all URLs have custom routes.

please help, because I am almost leaving development using the Zend framework !!

+3
source share
2

$this->_helper->json($appPrivArray)

setNoRender - . Javascript dataType: 'JSON', , .

+1

JSON jQuery ( ), " ". .

JSON , Content-Type application/json, .

setNoRender(true), script .

, :

public function getPrivilegesAction() {
    if ($this->getRequest()->isXmlHttpRequest()) {
        $this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->json($appPrivArray);
    }
}

- Javascript alert('hello')?

+1

All Articles