Sto cercando di restituire una risposta JSON da un controller in Symfony 2. Esempio di modulo, in Spring MVC È possibile ottenere una risposta JSON con annotazione @ResponseBody. Voglio ottenere una risposta JSON, nessun MTT se si tratta di un JSON Array o di un oggetto Json, quindi manipolarlo con javascript nella vista.Restituisce un array JSON da un controller in Symfony
cerco il codice successivo:
/**
* @Route(
* "/drop/getCategory/",
* name="getCategory"
*)
* @Method("GET")
*/
public function getAllCategoryAction() {
$categorias = $this->getDoctrine()
->getRepository('AppBundle:Categoria')
->findAll();
$response = new JsonResponse();
$response->setData($categorias);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
ma ho [{},{}]
come risposta nel browser. Provo anche con $response = new Response(json_encode($categorias));
, ma ottengo lo stesso risultato.
Hai provato a "restituire nuovo JSONResponse ($ dati);"? – Neal