索引.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
}
})
}
});
})
Controller :
public function updateDataAction(Request $request)
{
$amount = //db call
return ??
}
我对这段代码有点困惑。我有一个页面显示一些数据(金额),上面有一个过滤器,用户可以在其中操纵金额。使用过滤器时,如何使用新过滤的数据更改 amount.low?我被困在 Controller 中应该返回什么以及 javascript 中应该返回什么。
最佳答案
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));
}
关于javascript - Symfony|Twig : update data after ajax call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878963/