如何检索Ajax在Cakephp发送的数据? [英] how to retrieve data sent by Ajax in Cakephp?
本文介绍了如何检索Ajax在Cakephp发送的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在这个问题上呆了一整天。我试图做的是使用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控制器: / p>
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
}
}
我不知道如何获得start_time和end_time这2个值。
请帮帮我。提前致谢。
PS:im using 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
为您提供控制器中的发布数据。
$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");
}
});
这篇关于如何检索Ajax在Cakephp发送的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文