Jquery Symfony2:无法使用ajax提交表单

Jquery Symfony2:无法使用ajax提交表单,jquery,ajax,forms,symfony,Jquery,Ajax,Forms,Symfony,我正试图通过ajax调用提交一个使用symfony2创建的表单。 我知道有很多教程,但我认为我做的每件事都是正确的,仍然无法使它工作 以下是我在控制器中的代码: /** * @Route("/preferences", name="_mybundle_preferences") * @Template() */ public function preferencesAction(Request $request) { $em = $this->getDoctrine()-&

我正试图通过ajax调用提交一个使用symfony2创建的表单。 我知道有很多教程,但我认为我做的每件事都是正确的,仍然无法使它工作

以下是我在控制器中的代码:

/**
 * @Route("/preferences", name="_mybundle_preferences")
 * @Template()
 */
public function preferencesAction(Request $request)
{

    $em = $this->getDoctrine()->getManager();

    $allprefs = $em->getRepository('MyBundle:MyPrefs')
                ->findAllPrefs();

    // Creating the form
    $defaultData = array();
    $formpref = $this->container
            ->get('form.factory')
            ->createNamedBuilder(
                'formpref', 
                'form', 
                $defaultData, 
                array('validation_groups' => array())
            );

    // Input the search field 
    // (will be connected to a jQuery function to filter results)
    $formpref->add('search', 'search', array( 'mapped' => false) );

    // Populate the list of choice fields where the user can select its 
    // preferences
    foreach ( $allprefs as $mypref)
    {
            $fieldname = $mypref->getId();
            $formpref->add($fieldname, 'choice', array(
                'choices' => array( 
                    '3' => 'Very', 
                    '2' => 'Quite', 
                    '1' => 'Poor', 
                    '0' => 'None'
                    ), 
                'expanded'    => false, 
                'label'       => $mypref->getName(),
                ));
    }

    // Just temporary, I want to remove it through ajax
    $formpref->add('send', 'submit');    

    // Get the form
    $formpref = $formpref->getForm();

    // Handle the request
    $formpref->handleRequest($request);

    if ($formpref->isValid()) {

        // do stuff

        $response = array("success" => true);
        return new Response(json_encode($response));

    }

    $result = array(
      'forms' => $formpref->createView(),
      );

    return $result;

}
在我的树枝上:

<div id="div1" data-path="{{path('_mybundle_preferences')}}">
</div>

    {{ form(forms) }}
通过一些文件,我可以把内容插入代码中,而不是显示在这里 请注意,$formpref->isValid调用在表单被调用时从不返回true 使用ajax提交,但使用普通提交按钮提交时效果良好

我还尝试将结果发送到不同的路径,但结果相同

我做错了什么?我在兜圈子……

我认为$formpref.serialize可能是问题所在。请尝试以下代码序列化您的表单:

var values = {};
var $form = $("#formpref"); //I assume formpref is id of your form
$.each($form.serializeArray(), function (i, field) {
    values[field.name] = field.value;
});
然后使用以下方式发布所有内容:

$.ajax({
    data: values ,
    (...)
});

谢谢你的回答。不幸的是,我仍然得到同样的$formpref->isValid返回false。。。此外,$formpref->getErrorsAsString正在返回一个空字符串,其中包含我的解决方案和您的解决方案……作为表单错误,您会得到哪些错误?您是否通过ajax调用生成并传递了csrf令牌?多亏了您的解决方案,我终于找到了错误。我实际上没有发送任何数据。表单是以一种奇怪的方式生成的,id=formpref的div为空。我必须按姓名选择表格。当我看到id=formfood时,我感到困惑,但没有意识到它是空的。感谢您的时间和帮助。@Alberto:当有多个具有相同ID的DOM元素时,这可能是一个问题;
$.ajax({
    data: values ,
    (...)
});