1
I’m trying to read the data that is received via php
and I need to read these values separately. Return is an object json
, when I show on console using command console.log(data)
and the following structure appears:
{"clientes":[
{"idEntidade":"314","nmEntidade":"3D Inform\u00e1tica Ltda"},
{"idEntidade":"439","nmEntidade":"Academia BigBone Fitness"},
{"idEntidade":"308","nmEntidade":"Academia de Ginastica Forca e Saude Ltda - ME"},
{"idEntidade":"371","nmEntidade":"Academia Simetria"}
]}
But if I try to access a position with the command console.log(data[0]['nmEntidade'])
for example, the answer is undefined
.
Jquery code
$.getJSON('../caminho/do/json', function (data) {
console.log(data);
console.log(data[0]['nmEntidade']);
});
PHP method that returns the data. (I’m using the framework Symfony )
public function filtraClienteAction()
{
$em = $this->getDoctrine()->getManager();
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT idEntidade, nmEntidade FROM entidades ORDER BY nmEntidade");
$statement->execute();
$clientes = $statement->fetchAll();
$response = new JsonResponse(json_encode(array(
'clientes' => $clientes
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
Accept one of the answers Diego to close the question on the site.
– LeAndrade