0
votes

Okei.. I am experiencing a little problem with my ajax login.

When I first submit the login form, it is working fine.

  • Type correct username/password => logged in => working fine.
  • If type wrong username/password => Error message display => (wrong uname/pass)

But if I try to re-type the correct username and password after error message display, I can not submit the form again!?!

In Firebug it gives me a "response" with the full-page where the form i located..

I need to reload the page to be able to re-post the login details.

I think this have something to do with Security component, but disabling it is out of the question.

Any suggestions??

The basics of my login.

var data = $("#LoginAjax").serialize();
$.ajax({
        type: "post",
        url: "/users/ajax_login",
        data: data,
        dataType: "json",
        success: function(data){
            if (data == 0) {
                alert('wrong username/pass, try again!');
                //location.reload();
            }
        }
    });



public function ajax_login() {
   $this->autoRender = false;
    if ($this->request->is('ajax')) {
        $this->request->data['User']['password'] = $this->Auth->password($this->request->data['User']['password']);
        $user = $this->User->find('first', array('conditions' => array('username' => $this->request->data['User']['email'], 'password' => $this->request->data['User']['password'])));
            if($user){
                echo 1;     
            } else {
                 echo 0; 
            }
    } else {
    $this->Session->setFlash('No Dice!', 'flash/alert');
    $this->redirect($this->Auth->logout());
   }
   exit;
}
1

1 Answers

0
votes

Try adding echo $this->Js->writeBuffer(); at the end of your login view.

Why don't you use the JsHelper of CakePHP?