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

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条回答 默认 最新

相关推荐 更多相似问题