weixin_33713707 2017-03-25 12:04 采纳率: 0%
浏览 30

带有symfony3的Ajax Jquery

i don't know what is wrong with this code, no errors but it doesn't work . what i want is when i click on a table row, a textarea should appear containing >all informations[web page][1] . *this is the twig file: 1. the script

<script>
    function detailFunction() {
    $('#ligneSelectionnee').click(function () {

        id=$(this).val();


        $.ajax({
            type:"POST",
            url:'http://localhost/randonneeweb/web/app_dev.php/recDetailAjax',
            dataType:"json",
            data:{id:id},
            success : function (response) {
                if (response!=null){
                    document.getElementById('nothing').style.display="none";
                    document.querySelector("#zoneDetail").innerHTML=response.view;
                }
                else{
                    document.getElementById('nothing').style.display="block";
                }

            }
        })

    })}
</script>
  1. the table row which should be selected
<tbody>
        {% for m in reclamations %}

            <tr id="ligneSelectionnee" value="{{ m.idreclamation }}" onclick="detailFunction()">
                <td> {{ m.dateReclamation|date('Y-m-d') }}</td>
                <td> {{ m.typereclamation }}</td>
                <td> {{ m.sujetreclamation}}</td>
                <td> {{ m.message }}</td>
            </tr>
        {% endfor %}
        </tbody>
  1. the div where the details should appear
<div id="zoneDetail" style="margin-right: 20em">

    </div>

**this is the function in the controller:

public function afficherDetailsAction(Request $request)
{echo "hellooo";
    if($request->isXmlHttpRequest())
    {
        $id=$request->get('id');
        $em=$this->getDoctrine()->getManager();
        $reclamations=$em->getRepository('RandoPlusRandoBundle:Reclamation')->findBy(array('idreclamation'=>$id));
        dump($reclamations);
        if($reclamations!=null)
        {
            return new JsonResponse(
            ['view'=>$this->renderView('RandoPlusRandoBundle:Reclamation:detailAjax.html.twig',array('detail'=>$reclamations))],200
            );
        }
    }
    return new Response(null);
}

***the routing for the Ajax Function:

recDetailAjax:
path:     /recDetailAjax
defaults: { _controller: RandoPlusRandoBundle:Reclamation:afficherDetails }

****and finally the twig used by Ajax to load details:

<label>Description</label></br><textarea>Date : {{ detail.dateReclamation }}</br>type:{{ detail.typereclamation }}</br>Sujet:{{ detail.sujetreclamation }}</br>Message:{{ detail.message }}</textarea>
  • 写回答

1条回答 默认 最新

  • elliott.david 2017-03-26 10:59
    关注

    detailAjax.html.twig is not strict json out but your response is json. you have to change it.

    you can use browser consoles network sections when you try ajax. There are details about request in this tool.

    评论

报告相同问题?