【问题标题】:Using Ajax with cakephp doesnt work将 Ajax 与 cakephp 一起使用不起作用
【发布时间】:2013-12-03 01:48:45
【问题描述】:

我有一个这样的页面:

基本上,我选择2个日期并点击按钮,然后下面的数据会改变而不刷新这个页面。

这是控制器中的代码:

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

        $this->autoRender = false;

        //if ($this->request->isPost()) {
            print_r($this->request->data);
            // get values here 
            echo $from=( $this->request->data('start_time'));
            echo $to= $this->request->data('end_time');
            Debugger::dump($from);
            Debugger::dump($to);


        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from);
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        //Debugger::dump($products);
        $this->set('products',$products);
    //}
    }
    else
    {
        $from='27 November 2012';
        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from);
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        $this->set('products',$products);
    }

如果请求是 ajax,它会从 POST 中获取两个值 $from$to 并将它们传递给 SQL 查询。如果请求不是 ajax (意味着第一次访问这个页面,日期还没有选择), $from 和 $to 被分配默认值。

这是我的 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 },
        success: function(data){
            alert("success");
        }
    });
});
});

它从 2 个日期选择器中获取数据,然后将其作为 POST 方法发送到控制器。

我的问题是,在我选择 2 个日期并点击按钮后,什么也没有发生。数据不会根据日期而改变。 关于这个的任何想法。提前致谢。

【问题讨论】:

  • 您的网址似乎偏离了方向 - 您是否确认它确实采取了正确的行动?如果是这样,它会显示什么?您是否尝试过查看 Chrome 开发者工具(或类似工具)中的“网络”标签?
  • @Dave 当我点击按钮时,它向服务器发送了一个 POST 请求,其中包含 2 个值。我在 Firefox 中使用了萤火虫。 POST hottest_products 200 OK
  • 返回什么?错误信息? ...等等?
  • 它什么也没返回。除了 POST 请求。没有改变。您可以在这里查看cmpt470.csil.sfu.ca:8019/project/cakephp/orders/… 账号是:admin@hotmail.com 密码是123456。登录后请再次访问上面的链接。
  • @Dave 我不确定这段代码是否正确 $option['conditions']=array('Discount.start_time >='=>$from);但是通过传递 $from = '27 November 2012',它应该会显示一些数据。它适用于mysql,但在这里不起作用。

标签: javascript php jquery ajax cakephp


【解决方案1】:

打开页面并在控制台中运行以下命令时:

$(".tab_container").html("loaded from ajax");

产品表现在只显示“从 ajax 加载”。如果 products 表的内容是由它自己的模板生成的,你可以让 cakephp 仅在它是一个 ajax 调用时呈现该模板:http://book.cakephp.org/2.0/en/controllers.html

$this->render('/Path/To/ProductTable/');

如果您的 cakephp 在进行 ajax 调用时只输出产品表,您可以尝试运行以下代码:

var from = "2000-01-01";
var to = "2014-01-01";
$.ajax({
    url: "/project/cakephp/orders/hottest_products",
    type: 'POST',
    data: {"start_time": from, "end_time": to }
}).then(
  function(result){
    $(".tab_container").html(result);
  },function(){
    console.log("fail",arguments);
  }
);

【讨论】:

  • 所以你的意思是我需要用 JSON 或 text/html 格式化数据?通常,hottest_product 页面会向控制器发送一个 POST 请求。控制器使用该数据执行选择查询以从数据库中检索数据,然后将其发送回页面以显示数据。
  • @TungPham 您可以使用 JSON、XML、DOM 或仅文本来返回,只要 JavaScript 对其进行处理即可。如果你返回一个 DOM 部分,你可以用新返回的部分替换一些东西的 innerHTML。如果产品表在它自己的模板中,那么您可以渲染它并让 JS 使用返回的 html 设置该表容器的 innerHTML。我假设您想使用 JSON,但 html 会更容易实现。
  • 我只是想用一些简单的东西,只要我能让它工作。
  • 在页面上:cmpt470.csil.sfu.ca:8019/project/cakephp/orders/… 包含在类tab_container的div中的代码是由它自己的模板生成的吗?如果是这样,您可以让 cakephp 仅返回表格,然后使用返回的内容设置 innerHTML:$(".tab_container").html(response);
  • 整个代码在
    中,但我认为这不是问题,因为 POST 方法返回的内容类似于:Array ( [start_time] => 2013 年 12 月 2 日 [ end_time] => 2013 年 12 月 8 日)
    '2013-12-02'
    '2013-12-08'
    在 Cakephp 中,您可以使用 $this- 访问 POST 方法>request->data('name_of_variable');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
相关资源
最近更新 更多