2011-09-02 114 views
0

我目前正在为我的应用程序使用AJAX登录。在起始页的顶部有点击它时,打开与登录表单对话框登录链接,但我有几个问题:CakePHP Ajax登录验证失败

  1. 当用户输入错误或没有数据,没有错误显示
  2. 当我点击“登录”和JsHelper更新了“模板”,我再次点击“登录”,他将我重定向到http://www.url.com/users/login(这不应该发生)
  3. 当我输入正确的logindata时,重定向完成或显示一条消息。

我想我现在会发布所有需要的代码进行分析。

UsersController ::登录()

public function login() { 
     if($this->RequestHandler->isAjax()) { 
      $this->layout = 'ajax'; 
      $this->render('/elements/users/login'); 
     } 
     return $this->Auth->login(); 
    } 

元件/ AJAX/login.ctp 这是登录对话框的模板。

<?php echo $this->Session->flash(); ?> 
<fieldset> 
    <?php 
    echo $this->Session->flash(); 
    echo $this->Form->create(
      'User', 
      array(
       'controller' => 'users', 
       'action' => 'login' 
     ) 
    ); 
    echo $this->Form->input(
     'email', 
     array(
      'label' => 'E-Mail', 
      'style' => 'width: 270px;' 
     ) 
    ); 
    echo $this->Form->input(
     'password', 
     array(
      'style' => 'width: 270px;' 
     ) 
    ); 
    echo $this->Js->submit(
     'Login', 
     array('url' => array(
      'controller' => 'users', 
      'action' => 'login' 
     ), 
     'update' => '#loginContainer') 
    ); 
    echo $this->Form->end(); 
    ?> 
</fieldset> 

我觉得它有什么做的UsersController ::登录(),但我不知道到哪里去寻找。所以,也许你可以帮助我?谢谢:)

回答

1

book上验证::登录():

有一点要注意的是,您必须手动将用户重定向登录后作为loginRedirect不叫。 $这个 - > Auth->登录($数据)在成功登录后,0上失败则返回1

因此,一些大致相同:

if ($this->Auth->login() === 1) { 
    $this->redirect('/'); 
} else { 
    $this->set('status', 'Bad login details'); 
} 

*彻底未经检验

连接用JavaScript编写处理。

+0

非常感谢您的信息,我将它添加到我的代码中,但没有任何更改:/。昨天我为CakePHP安装了DebugKit,当我提交有效的logindata时,他向我显示“调试计时器信息”。这完全是令人困惑的。为什么显示?当我查看DebugKit的“请求”时,参数** isAjax **是* off *,但是为什么? O_O Btw。登录失败时仍然没有显示错误:/ – Fortuna

+0

这是更多示例代码,然后您可以将其插入到应用程序中。 – Ivo

+0

@福尔图纳 - 任何运气? – jagamot