且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Symfony的2 FOS用户捆绑引导模式AJAX登录

更新时间:2023-11-30 10:05:34

我已经找到了解决方案。这是我加入到我的javascript,

I have found the solution. Here is what I added to my javascript,

<script>
    $(document).ready(function(){
        $('#_submit').click(function(e){
            e.preventDefault();
            $.ajax({
                type        : $('form').attr( 'method' ),
                url         : '{{ path("fos_user_security_check") }}',
                data        : $('form').serialize(),
                dataType    : "json",
                success     : function(data, status, object) {
                    if(data.error) $('.error').html(data.message);
                },
                error: function(data, status, object){
                    console.log(data.message);
                }
            });
        });
    });
</script>

和这里是我的 onAuthenticationFailure 从我的处理方法,

And here is my onAuthenticationFailure method from my handler,

public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
    $result = array(
        'success' => false, 
        'function' => 'onAuthenticationFailure', 
        'error' => true, 
        'message' => $this->translator->trans($exception->getMessage(), array(), 'FOSUserBundle')
    );
    $response = new Response(json_encode($result));
    $response->headers->set('Content-Type', 'application/json');

    return $response;
}

我觉得这是从我的Ajax方法是错误的URL。谢谢您的建议。

I think that it was the URL from my Ajax method that was wrong. Thank you for your advices.