0
这个程序是基于Flask的,它允许用户点击'LIKE'按钮来为该帖子增加1个like_count。为什么我无法通过Flask项目中的AJAX获取回调?
如下,我不能POST'postid'到'/ like'函数并且无法从它获得回调。终端显示类型错误:
like() missing 1 required positional argument: 'postid'.
like.html:
<a href="#" onclick="like(this, {{ post.post_id }});">LIKE({{ post.like_count }})</a>
<script type="text/javascript">
function like(doc, postid){
$.ajax({
url:'{{ url_for('main.like') }}',
data:{postid:postid},
type:'POST',
success:function(callback){
var temp = 'LIKE' + callback;
$(doc).text(temp)
}
});
}
</script>
views.py:
@main.route('/like', methods=['POST'])
@login_required
def like(postid):
post = Post.query.filter_by(post_id=postid).first()
new_count = post.like_count + 1
post.like_count = new_count +1
db.session.add(post)
db.session.commit()
return Response(new_count)
谢谢指出! PS,它应该是“request.form.get”吗? – rogwan