0

i try to view some rows from a table. I have one AjaxController in Symfony, this controller have the following function:

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Rowoco\AllgemeinBundle\Entity\Place;
use Symfony\Component\HttpFoundation\JsonResponse;
.
.
.

public function defaultAjaxAction( $job )
{
    $user = $this->get('security.context')->getToken()->getUser();
    $userId = $user->getId();
    $result = $this->$job( $userId );

    // Initialisiert Serializer
    $encoder = [new JsonEncoder()];
    $normalizer = [new GetSetMethodNormalizer()];
    $serializer = new Serializer($normalizer, $encoder);
    $newData = $serializer->serialize($result, 'json');

    return new Response($newData);
}

public function getPlacelist( $iduser )
{
    $em = $this->getDoctrine()->getManager();
    $request = Request::createFromGlobals();

    //if i put here a return 123, then the output in the javascript-console would return 123
    $placeRepo = $em->getRepository( "RowocoAllgemeinBundle:Place" );
    $placeEntity = $placeRepo->findBy(
        array(),
        array(),
        $request->request->get( "limitCount" ),
        $request->request->get( "limitStart" )
    );
    return new JsonResponse(array('place' => $placeEntity));
}

In my js-file i call it with this function:

function getPlaces()
{
    var data = {};
    data['limitCount'] = 10;
    data['limitStart'] = 0;

    var url = $( "#pageparameter" ).data( "url-getplacelist" );

    $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: "json"
        })
        .done(function(resp){
            console.log(resp);
            $( "#viewPlaces").html(resp.place.description);
        })
        .error(function(){
            console.log("No connection");
        });

}

In the table, i have 2 rows and see them in the workbench.

I dont use a repository. I only using the basic functions from symfony.

1
  • And what is your question? Commented Sep 8, 2014 at 21:37

2 Answers 2

0

Use the built-in JsonResponse class to return a JSON response. So in your getPlaceList controller action, add something like this at the end:

use Symfony\Component\HttpFoundation\JsonResponse;

...

return new JsonResponse(array('place' => $placeEntity));
Sign up to request clarification or add additional context in comments.

2 Comments

$( "#viewPlaces").html(resp.place.description); But now i dont get a entry in the jquery-file. How i can now use the json?
Usually, you iterate over the response using jQuery .each(), like this: $.each(response, function(key, value) { ... }); .
0

i dont know why i´m using all the time

$resp = array ( 
   "foo" => "bar"
);


return @json_decode(@json_encode($resp),1);

but it always works very nice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.