从 Symfony 中的控制器返回 JSON 数组
我正在尝试从Symfony 2中的控制器返回JSON响应。表单示例,在Spring MVC中,我可以获得带有@ResponseBody注释的JSON响应。我想得到一个JSON响应,如果它是JSON数组或Json对象,则不要使用mtter,然后在视图中使用javascript操作它。
我尝试下一个代码:
/**
* @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;
}
但是我在浏览器中得到响应。我也尝试过,但我得到了同样的结果。[{},{}]
$response = new Response(json_encode($categorias));