如何使用 ajax 在控制器中调用 symfony 函数?
How to call symfony function in controller with ajax?
我想用 ajax
在我的控制器中调用一个函数
我真的是初学者,我不太明白我需要做什么
/**
* @Route("/Article/{id}/{vote}", name="article_vote")
*/
public function vote($idArticle, $vote, Request $request, ObjectManager $manager){
}
<script>
$(document).on('click', '.ajax', function(){
that = $(this);
$.ajax({
url:'{{ (path('don't know')) }}',
type: "POST",
dataType: "json",
async: true,
});
return false;
});
</script>
最后我的函数路径需要在 url :
然后我添加了一个成功和错误函数
{% block javascripts %}
<script>
$(document).on('click', '#ajax', function(){
that = $(this);
$.ajax({
url : '{{ path('post_like', {'id' : article.id}) }}',
type : 'POST',
dataType : 'html',
success : function(code_html, statut){
code = JSON.parse(code_html);
},
error : function(resultat, statut, erreur){
}
});
return false;
});
</script>
{% endblock %}
我需要添加一个 return json
/**
* @Route("/article/{id}/like", name="post_like")
*/
public function vote(Article $article, Request $request, ObjectManager $manager, ArticleLikeRepository $likeRepo) : Response {
return $this->json([
'code' => 200,
'likes' => $likeRepo->count(['article' => $article])], 200);
}
我想用 ajax
在我的控制器中调用一个函数我真的是初学者,我不太明白我需要做什么
/**
* @Route("/Article/{id}/{vote}", name="article_vote")
*/
public function vote($idArticle, $vote, Request $request, ObjectManager $manager){
}
<script>
$(document).on('click', '.ajax', function(){
that = $(this);
$.ajax({
url:'{{ (path('don't know')) }}',
type: "POST",
dataType: "json",
async: true,
});
return false;
});
</script>
最后我的函数路径需要在 url :
然后我添加了一个成功和错误函数
{% block javascripts %}
<script>
$(document).on('click', '#ajax', function(){
that = $(this);
$.ajax({
url : '{{ path('post_like', {'id' : article.id}) }}',
type : 'POST',
dataType : 'html',
success : function(code_html, statut){
code = JSON.parse(code_html);
},
error : function(resultat, statut, erreur){
}
});
return false;
});
</script>
{% endblock %}
我需要添加一个 return json
/**
* @Route("/article/{id}/like", name="post_like")
*/
public function vote(Article $article, Request $request, ObjectManager $manager, ArticleLikeRepository $likeRepo) : Response {
return $this->json([
'code' => 200,
'likes' => $likeRepo->count(['article' => $article])], 200);
}