Json 中的额外字段在 Zend 中使用 $.ajax
我正在尝试将 Ajax 与 Zend 框架结合使用。我遵循了这个教程并且它有效。我使用以下代码来获取数据:
$('#button').click(function() {
$.ajax({
url: './ajax/review/format/json',
dataType: 'json',
success: function(json_data){
alert('....');
}
});
});
解析的数据如下所示:
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 ) ) )
我得到的 Json 如下所示:
{
"data": {
"reviews": [
{
"reviewid": 3,
"userid": 1,
"locationid": 3
},
{
"reviewid": 2,
"userid": 2,
"locationid": 2
}
]
}
}
我不知道“数据”字段来自哪里。我想这与 Zend 从控制器解析数据到视图的方式有关,例如 $this->view->data = array(...)
希望我解释清楚,请帮我删除额外的“数据”字段。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我更喜欢使用 JSON 操作助手来发布数据 - 像这样:
它将删除布局、禁用视图渲染并发送正确的标头。
编辑:您还可以将一个变量分配给具有您想要的键的视图 - 例如:
它将从 JSON 中删除您不需要的“数据”...
I prefer to use JSON action helper to post data - like this:
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:
It will remove the "data" from JSON you don't want...
如果 Zend 没有提供相应的配置,则您无法删除数据字段。但是您可以查看该视图的代码并尝试继承它并更改功能或编写自己的视图,以便它对数据对象本身进行 JSON 化。
另一种方法是将所有数据属性直接放入模型中,而不是数据对象中。但是模型中一定存在某种会发生冲突的错误对象。
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.