根据本教程https://knpuniversity.com/screencast/symfony/javascript#play,我正在提出ajax请求:
myController.php
/**
* @Route("/pages/{slug}/heart", name="article_toggle_heart", methods={"POST"})
*/
public function toggleArticleHeart($slug){
$id = "5";
return new JsonResponse(['output'=> $id]);
}
mypage.html.twig
<div class="js-like-article-count">
result
</div>
<a class="js-like-article" href="{{ path('article_toggle_heart', {slug:page.slug}) }}">Click</a>
$(document).ready(function () {
$( ".js-like-article" ).on( "click", function(e) {
e.preventDefault();
var $link = $(e.currentTarget);
$.ajax({method:POST, url: $link.attr('href')}).done(function(data){
$('.js-like-article-count').html(data.output);
});
});
});
我希望通过单击CLICK
,文本result
会变成5
但是什么也没发生。在控制台中,我得到了错误:
ReferenceError:未定义POST
答案 0 :(得分:3)
尝试
<div class="js-like-article-count">
result
</div>
<a class="js-like-article" href="{{ path('article_toggle_heart', {slug:page.slug}) }}">Click</a>
$(document).ready(function () {
$( ".js-like-article" ).on( "click", function(e) {
e.preventDefault();
var $link = $(e.currentTarget);
$.ajax({method:"POST", url: $link.attr('href')}).done(function(data){
$('.js-like-article-count').html(data.output);
});
});
});