view data type as json

For programming and general questions on Zend Framework

view data type as json

Postby 5ou2lkjhl-rfisjg7w8e on Sun Feb 03, 2013 7:55 pm

Hi,
I've googled quite much, but cant get solution. The problem is, that when I use my action to return JSON data (no view created for it) the reciving it jquery script throws an error 200 ("SyntaxError: JSON.parse: Unable to parse value: <!DOCTY" - seems that recived data from called zend action is just html).

I found out, that I need to disable rendering for this view.

so my action looks like this:
Code: Select all
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);

//do some stuff

echo Zend_Json::encode(array('result' => 'success'));


also I've found out that I need to put this code into my controller init() function:
Code: Select all
     $ajaxContext = $this->_helper->getHelper('AjaxContext');
     $ajaxContext->addActionContext('savequestions', 'json')->initContext();



the only thing is, when I go to url navigating to this zend JSON returning action in browser, I get error:
Fatal error: Uncaught exception 'Zend_Loader_PluginLoader_Exception' with message 'Plugin by name 'Layout' in ....
( ! ) Zend_Loader_PluginLoader_Exception: Plugin by name 'Layout' was not found in the registry; used paths: Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/ in ...


I dont use any helpers (I dont know why would I, I am begginner and didnt need to use any helpers yet). I read that I should set helpers path in bootstrapper, but someone also wrote that this is done defaultly anyway.

Can someone help me? It is really difficult for me to understand why (if I should) should I create empty helper just to block it? Or maye, someone could write the correct code (complete, couldn't find such in google) to make jquery ajax queries in zend without helpers?

Greg
5ou2lkjhl-rfisjg7w8e
 
Posts: 2
Joined: Sun Feb 03, 2013 7:34 pm

Re: view data type as json

Postby 5ou2lkjhl-rfisjg7w8e on Sun Feb 03, 2013 10:40 pm

I've changed script file encoding and now finally I get response in application/json. but the script returns array except of json data.

here is my controller:
Code: Select all
function init() {
     
     $ajaxContext = $this->_helper->getHelper('AjaxContext');
     $ajaxContext->addActionContext('savequestions', 'json')->initContext();
     $this->_helper->viewRenderer->setNoRender();
    
if ($this->_helper->hasHelper('layout')) {
        $this->_helper->layout->disableLayout(); // disable layouts
    }
   
   }
   
   
  function savequestionsAction(){
 
      $result = array("first"=>"1", "second"=>"2", "thrid"=>"3");
      
      $this->_helper->json($result);
      
   }
   


and my browsers shows as an answer:
Content-Type: application/json

{"first":"1","second":"2","thrid":"3"}

it doesnt give me json objects but a string representing array structure,

Can anybody tell me why?
5ou2lkjhl-rfisjg7w8e
 
Posts: 2
Joined: Sun Feb 03, 2013 7:34 pm


Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 2 guests