繁体   English   中英

Symfony2 Ajax jQuery

[英]Symfony2 Ajax jQuery

我在尝试使Symfony和ajax相互配合时遇到一些问题。 我是Symfony和Ajax整个世界的新手,所以请放轻松。 我确定我的代码是一场噩梦,但我正在学习:)

我能够创建一个表单并将其数据发布到Symfony而不使用Ajax(通过URL发布),但是,当我尝试通过ajax(完全相同的表单)进行操作时,我得到了JSON响应400。我知道它必须Symfony无法理解即将发布的帖子数据。 在我的控制器操作中,它可以正常处理,直到到达“ if($ form-> isValid())”部分为止,这是Symfony不喜欢我的表单的地方。

这是我的jQuery ajax,用于通过表单发送发布数据:

 $(document).ready(function() {
        $("#myForm").submit(function(){

            // My form
            var $form = $(this).closest("#myForm");

            // If valid
            if($form){

                // The url where the form is being posted via ajax
                var url = $("#myForm").attr("action");

                // Post the data
                $.post(url,{
                    type: "POST",
                    data: $form.serialize(), // serializing the data being sent 
                    cache: false
                },function(data){

                    // If valid
                    if(data.responseCode == 200 ){
                        alert(data.responseCode);
                    }
                    else if(data.responseCode==400){
                        alert(data.responseCode);
                    }else{
                        alert("bad response all together...");
                    }
                }); 
            }
            return false;
        });
    });

这是通过Symfony表单构建器创建的表单:

 <form novalidate class="form-horizontal" id="myForm" action="/symfonydev/web/app_dev.php/warehouse/ajax/insert/" method="POST" >
 <input type="text" id="warehouse_name" name="warehouse[name]" required="required" placeholder="Location Name" class="input-block-level" value="" />
 <input type="text" id="warehouse_address" name="warehouse[address]" required="required" placeholder="Address" class="input-block-level" value="" />
 <input type="text" id="warehouse_city" name="warehouse[city]" required="required" placeholder="City" class="input-block-level" value="" />
 <input type="text" id="warehouse_state" name="warehouse[state]" required="required" placeholder="State" class="input-block-level" value="" />
 <input type="text" id="warehouse_zip" name="warehouse[zip]" required="required" placeholder="Zip" class="input-block-level" value="" />
 <input type="text" id="warehouse_email" name="warehouse[email]" required="required" placeholder="Email Address" class="input-block-level" value="" />
 <input type="text" id="warehouse_phone" name="warehouse[phone]" required="required" placeholder="Phone" class="input-block-level" value="" />
 <input type="text" id="warehouse_fax" name="warehouse[fax]" required="required" placeholder="fax" class="input-block-level" value="" />
 <input type="hidden" id="warehouse__token" name="warehouse[_token]" value="5352017c711a3a9d87ca9158334b32ab9f1dd3af" />
 <input type="submit" value="Send" />
 </form>

也许因为形式变量的序列化字符串是url编码的,Symfony不能解析这些变量以将其持久化到db吗? 当ajax通过序列化的字符串发布数据时,字段名称将像这样进行url编码。

 warehouse%5Bname%5D=Test&warehouse%5Baddress%5D=Test&warehouse%5Bcity%5D=Test&warehouse%5Bstate%5D=Test&warehouse%5Bzip%5D=Test&warehouse%5Bemail%5D=Test&warehouse%5Bphone%5D=Test&warehouse%5Bfax%5D=Test&warehouse%5B_token%5D=5352017c711a3a9d87ca9158334b32ab9f1dd3af

这是我的控制器动作:

 public function ajaxinsertAction(Request $request)
{

    // Get user's account
    $account = $this->getUser()->getAccount();

    // Warehouse form
    $warehouse = new Warehouse();
    $form = $this->createForm(new WarehouseType(), $warehouse);

    if ($request->isMethod('POST')) {

        // Get the Account of this user and set it on the warehouse being created.
        $account = $this->getUser()->getAccount();
        $warehouse->setAccount($account);
        $form->bind($request);

        if ($form->isValid()) {

            $em = $this->getDoctrine()->getManager();
            $em->persist($warehouse);
            $em->flush();

            $return = array("responseCode"=>200, "response"=>"Valid");
            $return = json_encode($return); // json encode the array
            return new Response($return,200,array('Content-Type'=>'application/json'));
            die;

        }else{

            $return = array("responseCode"=>400, "response"=>"Invalid");
            $return = json_encode($return); // json encode the array
            return new Response($return,400,array('Content-Type'=>'application/json'));
            die;
        }
    }
}

谢谢你的帮助!

有人对Symfony和Ajax有很好的教程吗? 我想了解更多有关此框架的信息,除了Symfony的文档(似乎很好,尽管关于ajax的文档很少)之外,似乎信息很少。

干杯!

对于那些在漫长的一天之后寻找类似问题的答案的人,我感到非常沮丧。

我当时处境非常相似。 问题是您要提交空数据,提交表单,然后拖到DOM。

var $form = $(this).closest("#myForm");

应该只是

var form=$(this); 

$.post(url, form.serialize(), function(data) {

});  

您是否尝试过手动传递值?

 ...
               $.post(url,{
                    type: "POST",
                    "warehouse[city]": $('#warehouse_city').val(),
                    "warehouse[state]" : $('#warehouse_state').val(),
                    ...
                    cache: false
                }
                ....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM