isAjax()是CakePHP 2.0上不推荐使用的方法.用什么方法代替那个方法?

Ken*_*iro 1 jquery cakephp-2.0

这是我的代码

    function add() {
    if(!empty($this->data)) {
        if($this->Post->save($this->data)) {
            if($this->RequestHandler->isAjax()){ //isAjax method is deprecated.
                //Handle Ajax
                $this->render('notif','ajax');
            } else {
                $this->Session->setFlash('Add successfully');
                $this->redirect(array('action'=>'index'));                    
            }
        }
        else {
            $this->Session->setFlash('Add failded please try again');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我读到该方法已被弃用http://book.cakephp.org/2.0/en/appendices/2-0-migration-guide.html?highlight=isajax

如何解决?

Dan*_*737 5

它说CakeRequest现在负责这一点.你可以在这里找到合适的段落:http://book.cakephp.org/2.0/en/controllers/request-response.html#inspecting-the-request

function add() {
    if(!empty($this->request->data)) {
        if($this->Post->save($this->request->data)) {
            if($this->request->is('ajax')){ 
                //Handle Ajax
                $this->render('notif','ajax');
            } else {
                $this->Session->setFlash('Add successfully');
                $this->redirect(array('action'=>'index'));                    
            }
        }
        else {
            $this->Session->setFlash('Add failded please try again');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)