douturan1807 2018-07-20 21:47
浏览 61


I am trying to perform an ajax post request but I can't handle the form.

Ajax :

function ft_postFormation(e) {
data = $('form[name=appbundle_formation_post]').serialize();

  url: ROOT_DIR + "api/back_office/post_formation",
  data: data,
  contentType: "application/json",
  dataType: 'json',
  beforeSend: function (xhr) {
    xhr.setRequestHeader("Authorization", "Bearer " + $.cookie("token"));
}).done(function (e) {

} });

controller :

    public function postFormationAction(Request $request)
    $formation = new Formation;
    $form = $this->createForm(FormationForm::class, $formation);

    if ($form->isSubmitted() && $form->isValid())
        //handling the form


I don't know if I serialize the data correctly or if I do not handle the form correctly. I read this solution everywhere but I am obviously missing something.

I receive the data within the controller (as a string). I also tried to serialize the form with $.serializeArray() but i could not handle the form either.

In all cases $form->getData says the form is correcly created but not filled with the datas received.

  • 写回答

1条回答 默认 最新

  • doucheng3811 2018-07-21 08:26

    I've finally found the problem.

    The header was wrong. If you use the serialize() Jquery function, the good header is not dataType:json but dataType:x-www-form-urlencoded. Then, the form is correctly add to the Request object and the form can be handled with $form->handleRequest($form).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 AD20 PCB板步线 这个要怎么步啊
    • ¥50 关于《奇迹世界》1.5版本,数据修改
    • ¥15 请问这个问题如何解决(关键词-File)
    • ¥50 visual studio 2022和EasyX图形化界面
    • ¥15 找一下报错原因,纠正一下
    • ¥50 Cox回归模型Nomogram图制作报错
    • ¥20 SQL如何查询多级用户的数据
    • ¥15 给车牌识别代码加一个识别轮廓长宽比的代码
    • ¥30 商品价格预测的transformer模型优化
    • ¥15 SQL中的图片,无法传回php到前端。没有任何报错。