Answer the question
In order to leave comments, you need to log in
How to correctly execute ajax request in Symfony?
I continue to study Symfony 5. There is a web page with news. At the bottom of the page is a list of comments, below them is the textarea "Comment text" and the "Submit" button with the code:
<input class="btn" value="отправить" data-path="{{ path('ajax_add_comment') }}" type="button">
$( function()
{
let but = $('.btn')
$( but ).bind('click', but_click )
...
function but_click()
{
let comment = $('#addComment').val()
let url= $( this ).data('path')
$.ajax({
type: "POST",
url: url,
data: {
comment : comment,
},
success: function(response) {
...
}
});
}
}
...
/**
* @Route("/ajax_add_comment", name="ajax_add_comment")
*/
public function addComment(Request $request, RouterInterface $router ): Response
{
$comment_text = $request->request->get('comment');
// запись в БД
...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question