ajax - 如何在Cakephp中检索Ajax发送的数据?

标签 ajax jquery cakephp

我被这个问题困扰了一整天。我想做的是使用 Ajax 将 2 个值从 View 发送到 Controller 。 这是我在 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 Controller :

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

        $this->autoRender = false;


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

我不知道如何获取 start_time 和 end_time 这两个值。 请帮我。提前致谢。 PS:我使用cakephp 2.3

最佳答案

$this->request->data 为您提供 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有一个错误,

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

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

关于ajax - 如何在Cakephp中检索Ajax发送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20327049/

相关文章:

php - CakePHP 权限错误

php - Cakephp MySQL数据库表列名

javascript - 来自 AJAX 调用的 knockout 数据未绑定(bind)

ajax - 事件不适用于动态创建的元素

jquery - Phonegap 链接无效

javascript - x 秒后自动打开 Bootstrap Modal

mysql - MySQL 中的逻辑列 - 如何?

javascript - 没有断点jquery代码无法工作

javascript - 如何在文本框未聚焦时关闭 JavaScript 文件?

javascript - 如何在我的函数中使用 .bind()