【问题标题】:Symfony2 Save Form Data via AjaxSymfony2 通过 Ajax 保存表单数据
【发布时间】:2016-01-13 21:03:49
【问题描述】:

我在 Symfony 中有一个需要通过 ajax 调用提交的表单。我编写了代码,但它没有在 db 中保存任何数据,但也没有给出/显示任何错误。

send_dict = {
    type: 'POST',
    url: $(this).attr('action'),
    processData: true,
    data: $('#Form').serialize(),
    beforeSend: function(request) {alert('before send');},
    success: function (data) {alert("success")},
    error: function(xhr, textStatus, thrownError) {
        alert('Some Thing Went Wrong, Please Refresh and Try Again...');
    }
}
$.ajax(send_dict);


public function createAction(Request $request)
{
    $user = $this->getUser();
    $address = new Addresses();

    if($request->isXmlHttpRequest()) {
        // Do something...
        if ($request->isMethod('POST')) {
            $request = $this->get('request');
            $permanent_is_present   = $request->get('permanent_is_present');
            $present_address  = $request->get('present_address');
            $present_address_country   = $request->get('present_address_country');
            // Persisting Objects to the Database
            if($permanent_is_present==true){
                $address->isIsPresent(true);
                $address->isIsPermanent(true);
            }else{
                $address->isIsPresent(true);
            }
            $address->setUser($user);

            $address->setStreet1($present_address);
            $address->setCountry($present_address_country);
            $address->setState($present_address_state);
            $address->setCity($present_address_city);
            //exit(\Doctrine\Common\Util\Debug::dump($address));
            // Entity Manager To Get Connected with Doctrine
            $em = $this->getDoctrine()->getManager();
            // Persists the entire objects....
            $em->persist($address);
            // Flush queries into database
            $em->flush();
            $output = array();
            $response = new Response();
            $output[] = array('success' => true);
            $response->headers->set('Content-Type', 'application/json');
            $response->setContent(json_encode($output));
            return $response;
        }else{
            return $this->render('AddressBundle:Addresses:new.html.twig');
        }
    } else {
        return $this->redirect($this->generateUrl('address_new'));
    }

}

发送前的警报和 ajax 函数的成功都显示。但是数据没有保存在数据库中? symfony 新手,也不知道如何跟踪/调试这个?

更新: 还有一件事我注意到系统中没有用户登录,并且他们试图进行 ajax 调用。它正在发送到控制器,因为找不到用户 ID,所以它没有保存到数据库中

【问题讨论】:

  • 可能是$(this).attr('action') 的范围界定问题 - 您希望this 指代什么?
  • 在控制器端创建表单对象,然后绑定请求。然后您可以正确验证。 heres 一个合理的资源来帮助你。
  • @Steve action="{{path('address_create')}}"
  • @DevDonkey 我已经使用 symfony 表单构建器完成了很多表单,但必须这样做。
  • @MTaqi 当然,但重点是this 不会引用属性所在的形式,因为它是在全局函数中调用的。 this 可能会引用窗口对象,它不会有动作属性

标签: javascript php jquery ajax symfony


【解决方案1】:

您需要序列化表单数据。然后给你的表单一个 Id 属性:

data: $('#Form').serialize(),

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多