2013-01-17 39 views
1

1應用程序。我正在使用ajax登錄並驗證用戶。我遇到的問題是cakephp 2.1 auth組件。當我手動登錄用戶的組件重定向,我不想發生,而是返回一個成功的代碼回到我的ajax請求。 有沒有辦法阻止這種默認行爲?使用ajax防止cakePHP 2.1 auth組件重定向

回答

2

AuthComponent正在調用Controller :: redirect(null,403)。你可以在你的AppController中覆蓋重定向():

/** 
* Proxy for Controller::redirect() to handle AJAX redirects 
* 
* @param string $url 
* @param int $status 
* @param bool $exit 
* @return void 
*/ 
    public function redirect($url, $status = null, $exit = true) { 
     // this statement catches not authenticated or not authorized ajax requests 
     // AuthComponent will call Controller::redirect(null, 403) in those cases. 
     // with this we're making sure that we return valid JSON responses in all cases 
     if($this->request->is('ajax') && $url == null && $status == 403) { 
      $this->response = new CakeResponse(array('code' => 'code')); 
      $this->response->send(); 
      return $this->_stop(); 
     } 
     return parent::redirect($url, $status, $exit); 
    } 
+0

謝謝我明白了。然而你的解決方案是正確的 –

+0

Pfff thanks ....什麼是hack =(這也適用於非ajax調用順便說一句(我需要403狀態碼,而不是在舊的項目中使用非ajax調用重定向) –