How to iterate with javascript a json response from symfony2

时间:2016-04-04 16:28:28

标签: javascript jquery json ajax symfony

i want to itarate a json response from symfony and put it in a table td>

my action :

    $search = $this->getDoctrine->...;
    $serializer = $this->get('serializer');

    foreach ($search as $key => $value) {

        $search[$key] = $serializer->serialize($value, "json");
    }

    $reponse = new JsonResponse($search);

    return $reponse;

this is what i have in my twig ( i examine it with Firebug ):

enter image description here

i have a to display at least something but sometimes i have undefined or nothing ... this my javascript function

$(document).ready(function () {

  var dataString = $("form").serialize();
  var typerequest = $("form").find('input[name="typerequest"]').val();
  $('#filtreperseance').ajaxForm({
     type: "POST",
     url: Routing.generate('myroute'),
     data: dataString,
     success: function (response) {

       $.each(response, function (cle, valeur) {
           $("#test").html(valeur);
       });                   

     }
  });
});

EDIT 1 : Console.log

enter image description here

EDIT 2 :

enter image description here

2 个答案:

答案 0 :(得分:0)

I'd try to cut down the problem. First make sure, the JSON is valid and looks the way you expect. Don't use jQuery at this point. Call the symfony controller in your browser directly. Then check the json. http://jsonviewer.stack.hu might be of use.

Once you verfified that the JSON itself is valid and contains what you need, we can look at the jQuery part. Then we'd need the code and the errors you get.

答案 1 :(得分:0)

当我从Controller返回JSOn时,我做错了。现在还可以