title: Symfony2 Ajax jQuery
body: <p>I am having a bit of an issue trying to get Symfony and ajax to play nicely with each other. I am newbie to this whole world of Symfony and ajax so go easy on me. I am sure my code is a nightmare, but I am learning :)  </p>
  
  <p>I am able to create a form and post my data to Symfony without Ajax (posting through a URL) But, when I try and do it via ajax (the exact same form) I get a json response of 400. I know it has to do with Symfony not being able to understand the post data that is coming into it. In my controller action, it processes fine until it gets to the section "if($form->isValid())", this is where Symfony does not like my form. </p>
  
  <p>Here is my jQuery ajax to send the post data via the form: </p>
  
  <pre><code> $(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;
          });
      });
  </code></pre>
  
  <p>Here is my form that was created via the Symfony form builder:</p>
  
  <pre><code> &lt;form novalidate class="form-horizontal" id="myForm" action="/symfonydev/web/app_dev.php/warehouse/ajax/insert/" method="POST" &gt;
   &lt;input type="text" id="warehouse_name" name="warehouse[name]" required="required" placeholder="Location Name" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_address" name="warehouse[address]" required="required" placeholder="Address" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_city" name="warehouse[city]" required="required" placeholder="City" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_state" name="warehouse[state]" required="required" placeholder="State" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_zip" name="warehouse[zip]" required="required" placeholder="Zip" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_email" name="warehouse[email]" required="required" placeholder="Email Address" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_phone" name="warehouse[phone]" required="required" placeholder="Phone" class="input-block-level" value="" /&gt;
   &lt;input type="text" id="warehouse_fax" name="warehouse[fax]" required="required" placeholder="fax" class="input-block-level" value="" /&gt;
   &lt;input type="hidden" id="warehouse__token" name="warehouse[_token]" value="5352017c711a3a9d87ca9158334b32ab9f1dd3af" /&gt;
   &lt;input type="submit" value="Send" /&gt;
   &lt;/form&gt;
  </code></pre>
  
  <p>Perhaps because the serialized string of form variables is url encoded, is Symfony not able to parse these out to persist them to the db? When ajax posts the data via the serialized string, the field names are url encoded like this.</p>
  
  <pre><code> warehouse%5Bname%5D=Test&amp;warehouse%5Baddress%5D=Test&amp;warehouse%5Bcity%5D=Test&amp;warehouse%5Bstate%5D=Test&amp;warehouse%5Bzip%5D=Test&amp;warehouse%5Bemail%5D=Test&amp;warehouse%5Bphone%5D=Test&amp;warehouse%5Bfax%5D=Test&amp;warehouse%5B_token%5D=5352017c711a3a9d87ca9158334b32ab9f1dd3af
  </code></pre>
  
  <p>This is my controller action: </p>
  
  <pre><code> public function ajaxinsertAction(Request $request)
  {
  
      // Get user's account
      $account = $this-&gt;getUser()-&gt;getAccount();
  
      // Warehouse form
      $warehouse = new Warehouse();
      $form = $this-&gt;createForm(new WarehouseType(), $warehouse);
  
      if ($request-&gt;isMethod('POST')) {
  
          // Get the Account of this user and set it on the warehouse being created.
          $account = $this-&gt;getUser()-&gt;getAccount();
          $warehouse-&gt;setAccount($account);
          $form-&gt;bind($request);
  
          if ($form-&gt;isValid()) {
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($warehouse);
              $em-&gt;flush();
  
              $return = array("responseCode"=&gt;200, "response"=&gt;"Valid");
              $return = json_encode($return); // json encode the array
              return new Response($return,200,array('Content-Type'=&gt;'application/json'));
              die;
  
          }else{
  
              $return = array("responseCode"=&gt;400, "response"=&gt;"Invalid");
              $return = json_encode($return); // json encode the array
              return new Response($return,400,array('Content-Type'=&gt;'application/json'));
              die;
          }
      }
  }
  </code></pre>
  
  <p>Thanks for your help! </p>
  
  <p>Any one have any good tutorials on Symfony and Ajax? I want to learn more about this framework, just seems to be such sparse info besides the docs at Symfony (which are pretty good, though they have little documentation on ajax). </p>
  
  <p>Cheers!</p>