dounai1986 2015-01-16 04:40
浏览 71
已采纳

如何从symfony2中的render函数获取json编码

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!

  • 写回答

2条回答 默认 最新

  • douzhantao2857 2015-01-16 11:46
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题