Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking JSON Objects from Zend Controllers Using JQuery in ZF2
    text
    copied!<p>I am new in Zend Framework. I am trying to display data from database using JSON. And I encoded the data and passed it to JQuery. But cannot retrieve value from database. Data displayed as "undefined". My controller function is as follows:</p> <pre><code> public function displayAction() { $data1 = array(); $request = $this-&gt;getRequest(); $response = $this-&gt;getResponse(); if ($request-&gt;isPost()) { $response-&gt;setContent(\Zend\Json\Json::encode(array('data' =&gt; $this-&gt; getStickyNotesTable() -&gt; fetchAll()))); } return $response; } </code></pre> <p>My FetchAll() is:</p> <pre><code> public function fetchAll() { $resultSet = $this-&gt;select(function (Select $select) { $select-&gt;order('created ASC'); }); $entities = array(); foreach ($resultSet as $row) { $entity = new Entity\StickyNote(); $entity-&gt;setId($row-&gt;id) -&gt;setNote($row-&gt;note) -&gt;setCreated($row-&gt;created); $entities[] = $entity; } return $entities; } </code></pre> <p>JQuery function : </p> <pre><code>function getUserList(element) { $('#indicator').show(); $.post('stickynotes/display', function(data, textStatus) { renderUserList(data); $('#indicator').hide(); }, "json" ); </code></pre> <p>}</p> <pre><code>function renderUserList(jsonData) { var table = '&lt;table width="600" cellpadding="5" class="table table-hover table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th scope="col"&gt;Note&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;'; $.each(jsonData, function(index, data){ table += '&lt;tr&gt;'; table += '&lt;td class="edit" field="note" user_id="'+data.id+'"&gt;'+data.note+'&lt;/td&gt;'; table += '&lt;td&gt;&lt;a href="javascript:void(0);" user_id="'+data.id+'" class="delete_confirm btn btn-danger"&gt;&lt;i class="icon-remove icon-white"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;'; table += '&lt;/tr&gt;'; }); table += '&lt;/tbody&gt;&lt;/table&gt;'; $('div#content').html(table); } </code></pre> <p>I tested it using Firebug. It shows </p> <pre><code>{"data":[{},{},{},{},{},{},{},{},{},{},{},{},{}]} </code></pre> <p>as Response. Anyone please help me. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload