FOSUserBundle (login / register) + AJAX + Symfony2

白昼怎懂夜的黑 提交于 2019-12-11 10:13:42

问题


I installed FOSUserBundle on my Symfony2 website and I overwrited the login and register pages. It's working very well, but now, I have another issue :

Making a pop up with an AJAX check for the login and register part, working with the different routes, controllers, etc.

How to do that the best way ?

My pop up is a simple bootstrap modal of login.html.twig page for FOSUserBundle, here is the code of the modal-body for the login part (I will use the register form for the register part) :

<div class="modal-body">
    {% trans_default_domain 'FOSUserBundle' %}

    {% block fos_user_content %}
        <form action="{{ path("fos_user_security_check") }}" method="post">
            <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

            <label for="username">{{ 'security.login.username'|trans }}</label>
            <input type="text" id="username" name="_username" value="" required="required" />

            <label for="password">{{ 'security.login.password'|trans }}</label>
            <input type="password" id="password" name="_password" required="required" />
            <br /><br />
            <input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans }}" />
        </form>
    {% endblock fos_user_content %}
</div>

I already searched on StackOverflow and other websites but didn't find the good answer I was looking for.

Thank you.


回答1:


no token when you use AJAX

user=$('#username').val();
pass=$('password').val();
$.ajax(
     {
     type: "POST",
     url: "{{ path ('your-rout')}}",
     data: {'user': user,'pass':pass},
      beforeSend: function() {
       },
      success: function(json) {}
});


来源:https://stackoverflow.com/questions/28611087/fosuserbundle-login-register-ajax-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!