Hello. The problem is as follows. There are two variables, blog_first_post_id = 7 and blog_last_post_id = 12, which are passed to a function using ajax.
$.ajax({ url: ajax_action_url, type: "POST", cache: true, async: true, contentType: "application/json; charset=utf-8", data: { "first_post_id": blog_first_post_id, "last_post_id": blog_last_post_id }, dataType: "json", beforeSend: function () { $('#progress-top').show(); }, success: function (response) { elegant_alert.success(response); }, complete: function () { $('#progress-top').hide(); }, error: function () { } }); The function in the controller accepts these variables successfully. And then I need to get content from the database, and return json with content in the response.
public function ajaxAction (Request $request) { $response = new Response(); $response->headers->set('Content-Type', 'application/json'); if ($request->request->get('first_post_id')) { $first_post_id = $request->request->get('first_post_id'); } if ($request->request->get('last_post_id')) { $last_post_id = $request->request->get('last_post_id'); } // функция getBlogContent получает данные постов из БД $blog_content = $this->getBlogContent($first_post_id, $last_post_id); // тут в ajax должна возвращаться переменная $blog_content в формате json, но как это сделать, я не знаю } Further, this content is displayed on the site page, but this is another question. I don’t know how to make an answer so that it is received via ajax in the JS variable, therefore I ask for your help.