Bonjour,

Je suis en train de faire de l'Ajax et je n'arrive pas à parser un tableau en JSON. On peut considérer ce topic un peut comme la suite du topic "Utiliser Ajax avec Symfony2" car je reprends un peu les même codes par contre, c'est un autre soucis.

Voilà les codes:

AjaxController.php

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
<?php
 
namespace Ajax\AppliBundle\Controller;
 
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Ajax\AppliBundle\Entity\Coffre;
 
class AjaxController extends Controller
{
    public function nombreObjetsRequeteAction()
     {
        //On récupère l'entityManager
        $em = $this->getDoctrine()->getManager();
 
        //On récupère le repository de Coffre
        $repositoryCoffre = $em->getRepository('AjaxAppliBundle:Coffre');
 
        //On récupère le coffre que l'on souhaite (1er enregistrement de la table)
        $listeCoffres = $repositoryCoffre->findAll();
 
        $tableauCoffre = array();
        $item = array();
 
        foreach($listeCoffres as $coffre)
        {
            $item['identifiant'] = $coffre->getId();
            $item['nombre_objets'] = $coffre->getNombreObjets();
            $item['couleur'] = $coffre->getCouleur();
        }
 
        //On met item comme premier élément de $tableauCoffre
        $tableauCoffre[0] = $item;
 
        //On encode en JSON
        $response = new Response(json_encode($tableauCoffre));
        $response->headers->set('Content-Type', 'application/json');
 
        return $response;
    }
 
    public function nombreObjetsVueAction()
    {        
        return $this->render('AjaxAppliBundle:Principale:affichage-coffre.html.twig');
    }
}
affichage-coffre.html.twig

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{% extends "::base.html.twig" %}

{% block body %}
{{ parent() }}

<h2>Test de la méthode AJAX</h2>
 
Le coffre contient <div id="nbobjets"></div> objets actuellement.
{% endblock %}
 
{% block javascripts %}

{{ parent() }}

<script type="text/javascript">
$(document).ready(function()
{ 
    var tableau_coffre = new Array();
    
    function valueOnline()
    {
        $.ajax(
        {
            type: 'GET',
            url: "{{ url('ajax_appli_coffre_requete') }}",
            success: function (data)
            {
                tableau_coffre = jQuery.parseJSON(data);

                identifiant_coffre = tableau_coffre[0].identifiant;

                $("#nbobjets").html(identifiant_coffre);
            }
        });
    }

    //Fonctionne
    setInterval(function() { valueOnline(); }, 1000);    
});
</script>
{% endblock %}
routing.yml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
ajax_appli_coffre_requete: 
    path:     /coffre-requete
    defaults: { _controller: AjaxAppliBundle:Ajax:nombreObjetsRequete }
 
ajax_appli_coffre_vue: 
    path:     /coffre-vue
    defaults: { _controller: AjaxAppliBundle:Ajax:nombreObjetsVue }
Du coup, j'ai fait un tableau avec un élément que j'aimerais parser avec la fonction jQuery.parseJSON mais je n'ai pas le résultat escompté.

Je ne vois pas pourquoi la valeur de tableau_coffre[0]['identifiant'] ne s'affiche pas dans le twig.

Voilà à quoi ressemble le tableau avec un var_dump:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
array (size=1)
  0 => 
    array (size=3)
      'identifiant' => int 4
      'nombre_objets' => string '94' (length=2)
      'couleur' => string 'vert' (length=4)
Pouvez vous m'orienter s'il vous plaît?

Je vous remercie par avance,