CakePHP - How to return string (like JSON) from controller action to Ajax request

后端 未结 4 1333
情书的邮戳
情书的邮戳 2021-01-06 04:58

So I have my JavaScript making an Ajax call to /my_controller/ajax_action but then in the controller I don\'t know what to do to output something back to the Ja

4条回答
  •  半阙折子戏
    2021-01-06 05:35

    try this:

    within your view folder for the corresponding controller(my_controller) make a folder named json and place a file named index.ctp and within that ctp file write this code:

    
    

    within your my_controller in index() wrote this code:

    $this->set('yourVariableNameReturnedFromController', $this->YOURMODEL->find('all'));
    

    within your app_controller.php(if not exist you have to made it) write this code

    function beforeFilter(){
      if ($this->RequestHandler->ext == 'json') {
         Configure::write('debug', 0);
      }
    }
    

提交回复
热议问题