Bonjour,
je suis entrain de faire un petit système de commentaire mais je suis bloqué puisque rien ne se passe du coté AJAX
voici ce que j'ai dans le contorleur
pour le JS:Code:
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
48 public function creerAction($id) { $video=$this->getDoctrine() ->getManager() ->getRepository('PlatformApplicationBundle:Video') ->find($id); $commentaire= new Commentaire(); $commentaire->setCreatedAt(new \DateTime()); $commentaire->setVideo($video); $form=$this->createForm(new CommentaireType, $commentaire); $request = $this->get('request'); // $form->bind($request); if($request->isXmlHttpRequest()) { if ($request->getMethod() == 'POST') { if ($form->isValid()) { $video->addCommentaire($commentaire); $em = $this->getDoctrine()->getManager(); $em->persist($video); $em->persist($commentaire); $em->flush(); $json=json_encode(array( 'video_id' => $id , 'commentaire'=>$commentaire,)); $data= new Response($json); $data->headers->set('Content-type','application/json'); return $data; }} } return $this->render('PlatformApplicationBundle:TV:commentaire.html.twig',array('form'=>$form->createView(), 'video_id' => $id, 'commentaire'=>$commentaire, )); }
et le formulaire avec un petit bloc pour faire le prepend en dessusCode:
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 <script type="text/javascript"> $(document).ready(function() { // Au submit du formulaire $('#form').submit( function() { var commentaire = $('#platform_commentaire_commentaire').val(); $('#commentaire').prepend('</strong> a dit :<br />'+commentaire+'</p>'); var dataString = 'commentaire='+ commentaire ; // On fait l'appel Ajax $.ajax({ type: "GET", url : "{{ path('platform_commentaire_ajout', {id: video_id}) }}", dataType : 'json', data: dataString, cache: false, success : function(data){ reponse($.map(data, function(objet){ return objet; })); } }) // On retourne false pour ne pas recharger la page return false; }); });
Merci d'avanceCode:
1
2
3
4
5
6
7
8 <form action="{{ path('platform_commentaire_ajout', {id: video_id}) }}" method="POST" {{ form_enctype(form) }} id="form"> {{ form_widget(form) }} <input type="submit" /> </form> <div id="commentaire"> <p class="last pair first" id="com_1"><strong>BN</strong> a dit :<br />Coucou ! Je m'appelle BN et je suis un BN, et toi, tu fais quoi dans la vie ?</p> </div>