how to retrieve data sent by Ajax in Cakephp?

后端 未结 2 410
时光说笑
时光说笑 2021-01-18 20:09

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

2条回答
  •  遇见更好的自我
    2021-01-18 20:40

    $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']; 
        }
    
    }
    

    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");
        }
    });
    

提交回复
热议问题