Symfony2 returns empty JSON when calling AJAX, and the variable is not empty

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!

+6
source share
4 answers

I managed to fix this using the Doctrine2 entity manager to get the result in an array, after which I started to encode it in JSON. I'm not sure if this is the cleanest way to do this (getEntityManager () seems deprecated according to my IDE), but now it works fine.

 public function ajaxAction() { $em = $this->getDoctrine()->getEntityManager(); $query = $em->createQuery('SELECT l FROM Snow\FrontBundle\Entity\Location l WHERE l.id=:id'); $query->setParameter('id', 1); $result = $query->getArrayResult(); return new Response(json_encode($result), 200); } 
+5
source

This is because json_encode() does not know how to serialize an object (other than StdClass ) into JSON. There are at least two ways to solve this problem:

  • If you are on PHP 5.4 or higher, you can implement your JsonSerializable object, which PHP will use when you call json_encode() on the object.
  • If you are using PHP 5.3 or earlier, you can use the Symfony Serializer component to convert the object to JSON using a number of different methods. I will not explain how to do this, since the documentation is pretty clear.
+2
source

Code example:

 $entity = // Get some entity $result = array( 'id' => $entity->getId(), 'name' => $entity->getName() ); return new Response(json_encode($result)); 
-1
source

If you want to receive data, you must use the query repository (dsl), and in the $query variable use the getArrayResult() method. This allows you to get the array directly.

-1
source

All Articles