douturan1807 2018-07-20 21:47
浏览 61
已采纳

处理Ajax请求Symfony

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

Ajax :

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


$.post({
  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) {
  console.log(e);
});

} });

controller :

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

    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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 session失效问题
      • ¥15 GMS地下水数值模拟无法打开
      • ¥15 树型结构的数据怎么匹配分支
      • ¥15 stm32F4用.引不出来结构体成员怎么解决
      • ¥15 .exe文件打不开怎么解决?
      • ¥30 Syntax Error: TypeError: this.getOptions is not a function的问题
      • ¥15 eclipse Android环境搭建
      • ¥15 unity中脚本之间的继承有什么作用
      • ¥15 C语言内存对齐代码不太理解
      • ¥15 uniapp中的uni-file-picker组件多图上传,删除,显示。以及C#.Net后端处理问题