【问题标题】:How to send data back from a controller to a view in cakephp?如何将数据从控制器发送回 cakephp 中的视图?
【发布时间】:2013-12-03 06:43:49
【问题描述】:

在我看来,我向控制器发送了一个 Ajax POST 请求以及数据。然后,在控制器中,它将接收数据并将其放入选择查询作为输入以从数据库中获取数据。然后,它将从数据库接收到的数据发送到视图以显示。

if( $this->request->is('ajax') ) {

        $this->autoRender = false;


            print_r($this->request->data);
            // get values from Ajax POST request here 
            $from=( $this->request->data('start_time'));
            $from = date('Y-m-d', strtotime($from));
            $to= $this->request->data('end_time');
            $to = date('Y-m-d', strtotime($to));
        //put data to a select query
                     .....
        //put all data received from database into $products
        $products = $this->Order->find('all',$option);
        Debugger::dump($products);
                    //send data to the view.
        $this->set('products',$products);
    //}
    }

我在 $products 中获得了所有我需要的数据,但是当我将其发送到查看时,它没有显示任何内容。通常,通常的方法是使用:

$this->set('products',$products);

但在这里,我用于 Ajax 请求:

$this->autoRender = false;

所以,这可能是数据没有显示的原因,但是我怎样才能发送数据来查看呢? 请帮帮我。

更新: 我的视图代码是:

<?php foreach ($products as $product): ?>

            <tr>    
                <td><?php echo $product ['Discount']['product_id']; ?></td>
                <td><?php echo $this->Html->link($product['Product']['product_name'], array('controller'=>'products', 'action' => 'view', $product['Discount']['product_id'])); ?></td>
                <td>$<?php echo $product ['Order']['benefit']; ?></td>
                <td><?php echo $product ['Order']['number']; ?></td>
                </td>
            </tr>

            <?php endforeach; ?>

还有我的 Ajax 脚本:

<script>
$(function(){
    $('#btnSubmit').click(function() {
    var from = $('#from').val();
    var to = $('#to').val();
    alert(from+" "+to);
    $.ajax({
        url: "/project/cakephp/orders/hottest_products",
        type: 'POST',

        data: {"start_time": from, "end_time": to },
        beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
        success: function(data){
            console.log(data);
        }
    });
});
});

【问题讨论】:

  • 首先你需要从你的控件中移除 autoRender
  • 好的。删除后,我看到fireBug的Response中的数据发生了变化,但页面中没有变化。
  • 现在在视图中调试 $products
  • debug $products on view 是什么意思?你的意思是'$this->set('products',$products);' ?
  • 试试这个调试($products);确保在 Config/core.php 中将调试模式设置为 2

标签: php jquery ajax cakephp cakephp-2.0


【解决方案1】:

您应该在名为 ajax.ctp 的布局中创建一个视图

内容如下:

<?php echo $content_for_layout?>

然后:

if($this->RequestHandler->isAjax()) {
      $this->layout = 'ajax';
}

【讨论】:

    【解决方案2】:

    你应该返回像

    这样的数据
        Debugger::dump($products);
                    //send data to the view.
        return $products;`
    

    建议return json_encode($products);并在成功方法中解码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2020-01-06
      • 2016-12-27
      • 2012-07-11
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      相关资源
      最近更新 更多