FOSUserBundle(登录/注册)+AJAX+Symfony2



我在Symfony2网站上安装了FOSUserBundle,并覆盖了登录和注册页面。它运行得很好,但现在,我有另一个问题:

使用AJAX检查登录和注册部分,使用不同的路由、控制器等进行弹出。

如何做到最好?

我的弹出窗口是FOSUserBundlelogin.html.twig页面的简单引导模式,这里是登录部分的modal-body的代码(我将使用注册部分的注册表单):

<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>

我已经在StackOverflow和其他网站上搜索过了,但没有找到我想要的好答案。

谢谢。

使用AJAX 时没有令牌

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

最新更新