【问题标题】:Symfony|Twig: update data after ajax callSymfony|Twig:ajax 调用后更新数据
【发布时间】:2017-05-09 20:06:58
【问题描述】:

Index.html.twig

{% if amount.low != 0 %}
    <p class="amount">{{ amount.low|round(0, 'floor') }}</p>
{% endif %}

Javascript:

$(document).ready(function () {
    $("#Filter1").change(function() {
        var input = $(this).val();

        if(inputCPU.length >= 1) {
            var data = {input: input};
            $.ajax({
                type: "POST",
                url: ROOT_URL + "default/update/data",
                dataType: 'json',
                timeout: 3000,
                success: function(response){
                    $(".amount").html(response.result); ???
                        console.log(response.result);

                },
                error: function() {
                    alert('Error with showing the filter.') //debug reasons
                }
            })
        }
    });
})

控制器:

public function updateDataAction(Request $request)
{


    $amount = //db call

    return ??
}

我有点卡在这段代码上。我有一个页面显示一些数据(金额),上面有一个过滤器,用户可以在其中操纵金额。使用过滤器时,如何使用新过滤的数据更改 amount.low?我被困在我应该在我的控制器中返回什么以及我的 javascript 中应该取得什么成功。

【问题讨论】:

    标签: javascript php jquery ajax symfony


    【解决方案1】:

    http://symfony.com/doc/current/components/http_foundation.html#creating-a-json-response

    use Symfony\Component\HttpFoundation\JsonResponse;
    
    // ...
    
    public function updateDataAction(Request $request)
    {
    
    
        $amount = //db call
    
        return new JsonResponse(array('amount' => $amount));
    
    }
    

    【讨论】:

    • 由于某种原因我没有得到任何结果?就像我不会进入 updateDataAction 方法一样。你知道问题可能是什么吗?
    • 您可以尝试直接从浏览器请求该路由/操作。
    猜你喜欢
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多