how to retrieve data sent by Ajax in Cakephp?

后端 未结 2 383
时光说笑
时光说笑 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:38

    If you method is POST:

     if($this->request->is('ajax'){
    $this->request->data['start_time'];
    $this->layout = 'ajax';
    }
    

    OR

    public function somefunction(){  
    $this->request->data['start_time'];  
    $this->autoRender = false;
    

    ANd if method is GET:

     if($this->request->is('ajax'){
    $this->request->query('start_time');
    $this->layout = 'ajax';
    }  
    

    OR

    public function somefunction(){  
    $this->request->query('start_time');  
    $this->autoRender = false;
    
    0 讨论(0)
  • 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");
        }
    });
    
    0 讨论(0)
提交回复
热议问题