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