Problem
I am trying to get an AJAX answer, so I can play with it to make it easier to use my forms. When I do the controller (the code below) returns a normal response with var_dump() , I get the output of the object, so I know that the request is not erroneous (I use ID 1 to request debugging). However, when I return the output using json_encode() , I just get an empty JSON file.
HTML form in view
<div id="content"> <form id="myForm" action="{{path('snow_ajax')}}" method="POST" > Write your name here: <input type="text" name="name" id="name_id" value="" /><br /> <input type="submit" value="Send" /> </form> </div>
Script in the same view
<script type="text/javascript"> $(document).ready(function() { $("#myForm").submit(function(){ var url=$("#myForm").attr("action"); $.post(url,{ formName:"ajaxtest", other:"attributes" },function(data){ if(data.responseCode==200 ){ alert("Got your json!"); } else{ alert("something went wrong :("); } }); return false; }); }); </script>
Controller with a normal response (works)
public function ajaxAction() { $location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location') ->find(1); $output = var_dump($location); return $output; }
Controller with AJAX response (not working, returns empty JSON)
public function ajaxAction() { $location = $this->getDoctrine()->getRepository('SnowFrontBundle:Location') ->find(1); return new Response(json_encode($location), 200); }
Can someone help me here please? It drives me crazy!
source share