I'm currently working on a symfony2 and i'm sendig a JSON information about my products from my controller
public function getProduitsOnJSONAction()
{
$em = $this->container->get('doctrine')->getEntityManager();
$produits = $em->getRepository('BTBundle:Produit')->findAll();
//start bloc reponse json
$encoders = array(new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$serializer = new Serializer($normalizers, $encoders);
// passed data $produits
$response = new Response($serializer->serialize($produits, 'json'));
$response->headers->set('Content-Type', 'application/json');
return $response;
// end bloc json response
}
the JSON from this URL ('http://localhost/BusinessTracker/web/app_dev.php/getProduitsJSON'):
[{"id":1,"refProduit":"1265466","nomProduit":"Yagourt","prixProduit":270,"stockProduit":10,"libelleProduit":"Yagourt D\u00e9lice"},{"id":2,"refProduit":"000001","nomProduit":"Nutella","prixProduit":4500,"stockProduit":15,"libelleProduit":"Chocolat Nutella"},{"id":3,"refProduit":"000002","nomProduit":"Lait D\u00e9lice","prixProduit":950,"stockProduit":30,"libelleProduit":"1L de Lait d\u00e9lice"}]
and now from the client side i want to decoding this JSON via ajax
$.getJSON('http://localhost/BusinessTracker/web/app_dev.php/getProduitsJSON', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
and nothing happens. Please i need a help