首先,您需要注册到控制器的路由:
app_bundle_route: path: /ajax_request defaults: { _controller: AppBundle:Main:index }然后在您的主视图中加载jQuery,也许您已经完成了。您需要在模板中调用号召性用语,并通过一些触发器来启动AJAX请求:
{% extends 'app/layout.html.twig' %}{% block content %} <button >click me!</button> <div id="ajax-results">here comes the result</div> <script> $(document).on('click', 'button.ajax', function(){ that = $(this); $.ajax({ url:'{{ (path('app_bundle_route')) }}', type: "POST", dataType: "json", data: { "some_var_name": "some_var_value" }, async: true, success: function (data) { console.log(data) $('div#ajax-results').html(data.output); } }); return false; }); </script>{% endblock %}至少您的控制器非常简单:
public function indexAction(Request $request){ if($request->request->get('some_var_name')){ //make something curious, get some unbelieveable data $arrData = ['output' => 'here the result which will appear in div']; return new JsonResponse($arrData); } return $this->render('app/main/index.html.twig');}我认为这个概念应该弄清楚它如何工作



