Ajax POST执行了两次

时间:2015-10-19 14:08:10

标签: javascript php jquery ajax symfony

我无法理解为什么这个Ajax POST被执行两次。我在其他线程中看到问题是浏览器正在添加'/'tp我的URL但不是我的情况。 这是我的js:

var Profile = function (params) {
    this.profilecontainer = params.profilecontainer;
    this.profileManagementURL = params.profileManagementURL;
    this.orderURL = params.orderURL;
    this.editProfile();
};


Profile.prototype.editProfile = function ()
{
    var self = this;
    $('.' + self.profilecontainer).on('submit',"form", function (e)
    {
       e.preventDefault();

       $.ajax({
           type: 'POST',
           url: self.profileManagementURL,
           data: $(this).serializeArray(),
           success: function (data) {
           },
           error: function (jqXHR)
           {
            }
    });

});
};

这是我的Twig:

{% block content %}
<div class="container content">
            <div>
                <div>
                    {{ render(path('profile_management')) }}
                </div>
            </div>
</div>
{% endblock %}

{% block javascripts %}
<script src="{{ asset('bundles/frontend/js/Profile.js') }}"></script>
<script type="text/javascript">

    $(function () {
        // Variables twig à passer aux scripts js
        var params = {
            profileManagementURL: '{{ path('profile_management') }}',
            orderURL: "{{ path('order_in_progress') }}",
            profilecontainer: 'container'
        };
        var oProfile = new Profile(params);
    });

</script>
{% endblock %}

现在这是我的控制器:

/**
 * @Route("/profile_management", name="profile_management")
 *
 * @param Request $poRequest Objet requête
 *
 * @return Response
 */
public function profileManagementAction()
{
    $oForm     = $this->createForm(ProfileForm());

    $oForm->handleRequest($poRequest);
    if ('POST' == $poRequest->getMethod())
    {
        if ($oForm->isValid())
        {  
        }
    }

    return     $this->get('templating')->renderResponse('FrontendBundle:Profile:userForm.html.twig', array(
                'form' => $oForm->createView(), $oResponse
                    ), $oResponse
    );
}

我使用相同格式的代码完成了很多功能,但我不知道这个功能有什么特别之处。

0 个答案:

没有答案
相关问题