Reputation: 219
I'm trying to use Ajax with Zend framework. I followed this tutorial and it works. I used following code to fetch the data:
$('#button').click(function() {
$.ajax({
url: './ajax/review/format/json',
dataType: 'json',
success: function(json_data){
alert('....');
}
});
});
The data parsed was like below:
Array ( [reviews] => Array ( [0] => Array ( [reviewid] => 3 [userid] => 1 [locationid]
=> 3 ) [1] => Array ( [reviewid] => 2 [userid] => 2 [locationid] => 2 ) [2] => Array (
[reviewid] => 1 [userid] => 1 [locationid] => 1 ) ) )
The Json I got was something like following:
{
"data": {
"reviews": [
{
"reviewid": 3,
"userid": 1,
"locationid": 3
},
{
"reviewid": 2,
"userid": 2,
"locationid": 2
}
]
}
}
I don't know where the "data" field comes from. I guess it relates to the way Zend parse data from controller to view, e.g. $this->view->data = array(...)
Hope I explained clearly, please help me to remove the extra "data" field.
Upvotes: 0
Views: 380
Reputation: 11217
I prefer to use JSON action helper to post data - like this:
// in controller
$this->_helper->json($dataToSend);
It will remove layout, disable view rendering and send proper headers.
Edit: You can also assign a variable to the view that has the key you desire - eg:
$this->view->reviews = $data;
It will remove the "data" from JSON you don't want...
Upvotes: 1
Reputation: 519
You can not remove the data field if Zend does not provide a configuration for that. But you can look into that view's code and try either inheriting from it and changing functionality or writing you own view, such that it JSONifies data object itself.
Other way is to put all of your data attributes in the model directly instead of data object. But then there must be some kind of error object inside your model that would conflict.
Upvotes: 0