Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Symfony表单AJAX返回空对象_Javascript_Php_Ajax_Forms_Symfony - Fatal编程技术网

Javascript Symfony表单AJAX返回空对象

Javascript Symfony表单AJAX返回空对象,javascript,php,ajax,forms,symfony,Javascript,Php,Ajax,Forms,Symfony,我试图用Symfony创建AJAX表单,但我的表单返回空对象。当我发送手动编写的文本或数组时,一切都正常。臭虫在哪里?我的表单或javascript代码有问题吗 /** * Renders the "new" form * * @Route("/", name="demo_new") * @Method("GET") */ public function newAction(Request $request) { $entity = new Demo(); $form = $t

我试图用Symfony创建AJAX表单,但我的表单返回空对象。当我发送手动编写的文本或数组时,一切都正常。臭虫在哪里?我的表单或javascript代码有问题吗

/**
* Renders the "new" form
* 
* @Route("/", name="demo_new")
* @Method("GET")
*/
public function newAction(Request $request) {
    $entity = new Demo();
    $form = $this->createForm(DemoType::class, $entity);

    return $this->render('default/new.html.twig', array(
            'entity' => $entity,
            'form' => $form->createView()
            )
    );
}

/**
*
* @Route("/", name="demo_create")
* @Method("POST")
*
*/
 public function createAction(Request $request) {
if (!$request->isXmlHttpRequest()) {
  return new JsonResponse(array('message' => 'You can access this only using Ajax!'), 400);
}

$entity = new Demo();
$form = $this->createForm(DemoType::class, $entity, array(
    'action' => $this->generateUrl('demo_create'),
    'method' => 'POST',
));

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
  $data = $form->getData();
  return new JsonResponse(
          [
            'message' => 'Success!',
            'data' => $data 
          ], 200);
}

$response = new JsonResponse(
        array(
    'message' => 'Error',
    'form' => $this->renderView('default/new.html.twig', array(
        'entity' => $entity,
        'form' => $form->createView(),
    ))), 400);
return $response;
 }
}
和Javascript代码:

  function initAjaxForm()
    {
    $('body').on('submit', '.ajaxForm', function (e) {

    e.preventDefault();

    $.ajax({
        type: $(this).attr('method'),
        url: $(this).attr('action'),
        data: $(this).serialize()
    })
    .done(function (data) {
        if (typeof data.message !== 'undefined') {
            console.log(data.data);
            console.log(data.message);
        }
    })
    .fail(function (jqXHR, textStatus, errorThrown) {
        if (typeof jqXHR.responseJSON !== 'undefined') {
            if (jqXHR.responseJSON.hasOwnProperty('form')) {
                $('#form_body').html(jqXHR.responseJSON.form);
            }

            $('.form_error').html(jqXHR.responseJSON.message);

        } else {
            alert(errorThrown);
        }

    });
});
}

今天版本2.8也有同样的问题,我会把它留在这里,以防它最终会伤害到其他人。我已经将它添加到我的表单生成器中

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return '';
}
“我的表单返回空对象”。您是说
$(this).serialize()
为空?或者PHP代码的响应是空的?请你确切地说明问题出在哪里。