CakePHP 2.5 JQuery Ajax登录元素

时间:2014-06-09 19:44:28

标签: jquery ajax cakephp-2.0

好的,所以我必须错过一些关键或基本的东西。我正在尝试创建一个使用JQuery / AJAX登录的登录元素。

在我的布局上,我有:

    //...
    echo $this->Html->script('jquery'); // Include jQuery library
    //...
    if (AuthComponent::user('id')){ 
        echo $this->element('userElement',array('username'=>AuthComponent::user('username')));
    } else {
        echo $this->element('loginElement');
    }
    echo $this->Js->writeBuffer();

UserElement只是说“Hi $ username”,当我通过转到/ users / login /

手动登录时工作正常

这是loginElement.ctp,我认为问题在于:

<?php
    $data = $this->Js->get('#UserForm')->serializeForm(array('isForm' => true, 'inline' => true));
    $this->Js->get('#UserForm')->event(
        'submit',
        $this->Js->request(
            array('action' => 'save'),
                array(
                    'update' => '#loadingdiv',//what to update while ajaxing
                    'before' => "$('#loading').fadeIn();",//before request
                    'complete' => "$('#loading').fadeOut();",//when request complete
                    'data' => $data,
                    'async' => true,    
                    'dataExpression'=>true,
                    'method' => 'POST',
                 )
            )
        );
?>
<div class="loadingdiv" id="loadingdiv"> </div>
<div class="users form">
    <?php
        echo $this->Session->flash('auth');
        echo $this->Form->create('User',array('action' => 'save', 'default' =>     false));
    ?>
    <fieldset>
    <?php
        echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
    <?php
        echo $this->Form->end(__('Login'));
        echo $this->Js->writeBuffer();
    ?>
</div>

表单在浏览器控制台中没有做任何事情,没有登录,没有失败的javascript调用....没有。我错过了什么?

1 个答案:

答案 0 :(得分:0)

回答我自己的问题,以便如果其他人偶然发现了这个问题,他们可以看到错误以及如何解决问题:

这里的两个关键部分是:

//create the form
echo $this->Form->create('User',array('action' => 'save', 'default' =>     false));

//bind the event
$this->Js->get('#UserForm')->event(
//...

我将事件绑定到表单&#34; UserForm&#34;,但是当我创建表单时,我会给它一个&#34;保存&#34; ..所以蛋糕自动构建的表单名称是&#34; UserSaveForm&#34;

当我将绑定更改为&#34; UserSaveForm&#34;有效。

[解决此问题后注意&#39;保存&#39;看起来很奇怪,所以我将表单动作,绑定和控制器功能更改为“登录”#39;所以我最终得到了表格&#34; UserLoginForm&#34;哪个读得好多了]