I Have two problem in this Case :
I want to pass a JSON object that i created in Zend Controller
public function exampleAction() {
$answers = array();
for($i = 0 ; $i < 3 ; $i++)
{
$answer = new Answer();
$answer->answer_id = 5 ; // for example
$answer->thanked = 'true';// for example
$answers[] = $answer;
}
echo Zend_Json_Encoder::encode($answers);
}
the Jquery Post function is :
$.post(
"/memberactions/getthanks/",
{values:values},
function(res){
alert(123);
}
, 'json')
First Question :
why the return response is HTML ? the response must be in JSON ?
Second Question
the HTML response is like this
[{"__className":"Answer","thanked":"true","answer_id":"5"}]
How can I make the response like this :
- answer
thanked : true
answer_id : 5
as a JSON Object without the __className:”Answer” (does it hurt to have the class name in the response) ?
Have you disabled layout, viewRenderer etc.? Also, you should send appropriate headers. You can do all this at once using the JSON action helper:
$this->_helper->json($answers);You could provide a
toArray()method inAnswer, which would return an array of relevant properties and then use it in your action:$answers[] = $answer->toArray();