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.

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退