S
S
Sergey2014-08-12 04:25:10
JavaScript
Sergey, 2014-08-12 04:25:10

How to do ajax authorization for FOSUserBundle on Symfony2?

It is necessary to check the entered fields and return a response in an arbitrary format with a specific error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DeFacto, 2014-08-12
@karmis

$(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);
                }
            });
        });
    });

and server side:
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;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question