我整整一天都被这个问题困扰。我试图做的是使用Ajax从视图向控制器发送2个值。这是我的代码hot_products:
hot_products
<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控制器:
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
$this->request->data 为您提供控制器中的帖子数据。
$this->request->data
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"); } });