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

后端 未结 4 1332
情书的邮戳
情书的邮戳 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:24

    Easiest way I found was to disable the automatic rendering:

    function ajax_action($data = null) {
        if($this->RequestHandler->isAjax()) {
            $this->autoRender = false;
            //process my data and return it
            return $data;
        } else {    
            $this->Session->setFlash(__('Not an AJAX Query', true));
            $this->redirect(array('action' => 'index'));
        }   
    }
    

提交回复
热议问题