gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:01:27 24 4
gpt4 key购买 nike

我被这个问题困扰了一整天。我想做的是使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com