从控制器获取数据,然后通过ajax打印

I have print action, witch get all my records from db

public function printAction()
{
    $users = $this->getDoctrine()->getRepository('ModelBundle:Users')->findAll();
    $resp = new JsonResponse($users, 200);
    return $resp;
}

I want use this data and print via ajax in div element, but i cant understand, how i can do it. Maybe anybody know. Please, help me.

Do you have a serializer in your user entity? Because you are getting all users as entities. I suggest you to do something like this:

When you click a button or any element, call your route like this:

//change this element to your button
$('a.press-me').click(function() {
    $.ajax({
        url: 'YOUR ROUTE HERE',
        dataType: 'HTML',
        success: function (data) {
            $('#your-div').html(data);
        }
    });
});

//create a view users.html.twig or something like that...
{% if users is defined and users is not empty %}
    {% for user in users %}
        Name: {{ user.name }}
    {% endfor %}
{% else %}
    <p class="error">No data!</p>
{% endif %}

//add this line to top of your controller
use Symfony\Component\HttpFoundation\Request;

//add request variable to your action
public function printAction(Request $request)
{
    //you want to get users via ajax right? so check if the request is ajax or not
    if($request->isXmlHttpRequest()) {
        // Get all users
        $users = $this->getDoctrine()->getRepository('ModelBundle:Users')->findAll();

    //render a view and pass it as variable
    return $this->renderView('ModelBundle:Default:users.html.twig', ['users'] => $users);
    }
}

Suppose in your view,

<div class="dynamic-data">

</div>


$.ajax({
         url: "path to your print function",
         type: "POST",
         dataType: "HTML",
         async: false,
         data: {"u": url},
         success: function(data) {
                  $(".dynamic-data").html(data);  
               // here directly manipulate the data in controller or get the data in success function and manipulate .                   
          }
      });

in print function instead of return print the data

Use jquery ajax() function to render data to html tag.The best way is to create a generic methon as follow and class it with url and div id

function renderPartialInDiv(url, divID) {
    $.ajax({
        cache: false,
        async: true,
        type: "POST",
        url: url,
        data: null,
        success: function (data) {
            $(divID).html(data);
        },
        processData: false,
        async: false
    });