dongzai3917 2016-08-22 14:06
浏览 53
已采纳

symfony与json ajax的响应

I'm new in Symfony i wanted to do some manipulation such I can get a list of all elemets in the entity Theme via Ajax but the answer is still "undefined" Here the code text strong

vue

    $(document).ready(function () {
        var $theme = $('#theme');
        $theme.append('<option value="">Choisir un thème</option>');
        $.ajax({
            type: 'post',
            url: '{{ path('web_site_liste_theme_cmb') }}',
            dataType: 'json',
            beforeSend: function () {
                $('#themediv').append('<div id="loading" style=" float: left; display: block;"><img src="{{ asset('bundles/BackBundle/img/loadingicon.gif') }}"/></div>');
            },
            success: function (json) {
                {#$('#theme').append({{  dump(json)}});#}
                console.log(json.value);
                $.each(json, function (index, value) {

                    //console.log(value);
                    $('#theme').append('<option value="' + value.id + '">' + value.name + '</option>');
                    $('#loading').remove();
                });
            }
        });
    });

Controller

  public function ListeThemeAction(Request $request)
{

    $em = $this->getDoctrine()->getEntityManager();
    if ($request->isXmlHttpRequest()) {
        $themes = $em->getRepository('WebSiteBackBundle:theme');
        $themes = $themes->findAll();
        //var_dump($themes);

        return  new JsonResponse($json);
    }
    return new JsonResponse('no results found', Response::HTTP_NOT_FOUND); // constant for 404

}

the server response is 200 OK, everything seems to work, I have the same number of data in the database but I can't read the objects values

and here is the : console.log(json)

  • 写回答

2条回答 默认 最新

  • dongxi4235 2016-08-22 16:05
    关注

    well, i found the answer i had to change the controller to make it happen. thank you Yonel ! here is a link that helped me Json in controller

    Controller

    public function ListeThemeAction(Request $request)
    {
    $output=array();
        $em = $this->getDoctrine()->getEntityManager();
        if ($request->isXmlHttpRequest()) {
            $themes = $em->getRepository('WebSiteBackBundle:theme');
            $themes = $themes->findAll();
            foreach ($themes as $theme){
    
                $output[]=array($theme->getId(),$theme->getName());
            }
         /*   var_dump($themes);
            $json = json_encode($themes);
    
            $response = new Response();*/
            //            return $response->setContent($json);
            return new JsonResponse($output);
    
        }
        return new JsonResponse('no results found', Response::HTTP_NOT_FOUND);
    }
    

    Vue

            $(document).ready(function () {
            var $theme = $('#theme');
            $theme.append('<option value="">Choisir un thème</option>');
            $.ajax({
                type: 'post',
                url: '{{ path('web_site_liste_theme_cmb') }}',
                dataType: 'json',
                beforeSend: function () {
                    $('#themediv').append('<div id="loading" style=" float: left; display: block;"><img src="{{ asset('bundles/BackBundle/img/loadingicon.gif') }}"/></div>');
                },
                success: function (json) {
    
                    console.log(json);
                    $.each(json, function (index, value) {
    
                        //console.log(value);
                        $('#theme').append('<option value="' + value[0]+ '">' + value[1] + '</option>');
                        $('#loading').remove();
                    });
                }
            });
        });
    

    Hoping it will help someone. thank you all for your answer it helped me a lot

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog