如何在 Cakephp 中检索 Ajax 发送的数据? [英] how to retrieve data sent by Ajax in Cakephp?

查看:16
本文介绍了如何在 Cakephp 中检索 Ajax 发送的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个问题困了一整天.我想要做的是使用 Ajax 将 2 个值从视图发送到控制器.这是我在 hot_products 视图中的代码:

I have been stuck at this problem for a whole day. What im trying to do is to send 2 values from view to controller using Ajax. This is my code in hot_products view:

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

        data: {"start_time": from, "end_time": to,
        success: function(data){
            alert("success");
            }
        }
    });
});
});

和我的 hot_products 控制器:

and my hot_products controller:

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

        $this->autoRender = false;


                    //code to get data and process it here
      }
}

我不知道如何获得 2 个值,即 start_time 和 end_time.请帮我.提前致谢.PS:我使用的是 cakephp 2.3

I dont know how to get 2 values which are start_time and end_time. Please help me. Thanks in advance. PS: im using cakephp 2.3

推荐答案

$this->request->data 为您提供控制器中的 post 数据.

$this->request->data gives you the post data in your controller.

public function hottest_products()
{   
    if( $this->request->is('ajax') ) {
        $this->autoRender = false;
    }

    if ($this->request->isPost()) {

        // get values here 
        echo $this->request->data['start_time'];
        echo $this->request->data['end_time']; 
    }

}

更新你的 ajax 出错了,

Update you've an error in your ajax,

$.ajax({
    url: "/orders/hot_products",
    type: 'POST',

    data: {"start_time": from, "end_time": to },
    success: function(data){
        alert("success");
    }
});

这篇关于如何在 Cakephp 中检索 Ajax 发送的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆