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

时间:2011-10-29 13:55:24

标签: 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');
        }
    }
}

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

如何解决这个问题?

1 个答案:

答案 0 :(得分: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');
        }
    }
}