Json 中的额外字段在 Zend 中使用 $.ajax

发布于 2024-11-08 13:27:11 字数 1193 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2024-11-15 13:27:11

我更喜欢使用 JSON 操作助手来发布数据 - 像这样:

// in controller
$this->_helper->json($dataToSend);

它将删除布局、禁用视图渲染并发送正确的标头。

编辑:您还可以将一个变量分配给具有您想要的键的视图 - 例如:

$this->view->reviews = $data;

它将从 JSON 中删除您不需要的“数据”...

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...

不羁少年 2024-11-15 13:27:11

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文