简体   繁体   中英

how to get json encode from render function in symfony2

I'm trying to work with ajax in symfony but is some hard for me. I have a form in twig where I get a value that I'm using for search a product in my database:

This is the form in twig:

<form id="myForm" method="post" action="{{ path('my_app_greeting') }}"> <div class="input-group"> <input id="name_id" name="name" type="text" class="form-control" placeholder="Buscar repuesto..." required /> <span class="input-group-btn"> <button id="search-button" class="btn btn-default" type="button">Buscar</button> </span> </div><!-- /input-group --></br> </form>

This is my code using jquery in the same twig template for send the data with ajax:

$(document).ready(function(){
    $("#myForm").submit(function(){
    var value = $("#name_id").val();
    $.ajax({
        type: "POST",
        data: { productValue: value },
        url: "/ventas/test/ajax/"
    })
    .done(function(response){
        template = response;
        $('#output').html(template.content); 
    })
    .fail(function(jqXHR, textStatus, errorThrown){
        alert('Error : ' + errorThrown);
    });
    return false;
    });
});

Later I read about how symfony works with ajax and I thought the best solution for my "problem" is create a new twig template called "ajaxContent.html.twig" for example and do this with the controller:

public function testAction()
{
    $request  = $this->get('request');
    $name     = $request->request->get('productValue');
    $template = $this->renderView('AcmeDemoTwoBundle:Ventas:ajaxContent.html.twig', array('value' => $name));
    $json     = json_encode($template);
    $response = new Response($json, 200);
    $response->headers->set('Content-Type', 'application/json');
    return new Response($response);
}

Of this way later I want to put all the html content inside my original template but I have a problem with json encode. I don't understand very well how it works and how is the better way for receive the data and show it in the twig template. How can I do this? ( I tried to show the data using $('#output').html(template.content); but doesn't work).

Thanks for your help!

Your code in controller seems to be not good.

I mean if you want to return json then what for do you render html (not json) twig template? That's the first.

Then you can get json response by using JsonResponse instead of setting header for standard response.

I don't know what's happening in ajaxContent.html.twig, but I would do something like this:

public function testAction(Request $request)
{
    $name     = $request->get('productValue');
    $response = $this->get('my_response_sercive')->giveMeResponseForTestAction($name);

    return new JsonResponse($response);
}

If you are waiting for html response in you javascript you have to set option dataType: 'html' for your ajax request. Then in testAction you don't have to set 'application/json' header and don't have to json_encode $template variable - but just render your template as you are doing it now. So in this case you could do something like this:

public function testAction(Request $request)
{
    $name     = $request->get('productValue');

    return $this->renderView('AcmeDemoTwoBundle:Ventas:ajaxContent.html.twig', array('value' => $name));
}

After that you can put html into your #output block directly:

$('#output').html(response);

您应该首先使用表单数组解决问题,

$template['content']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM