为什么我不能在Flask项目中通过AJAX获得回调?

2024-06-28 20:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这个程序是基于Flask的,它允许用户点击“LIKE”按钮来增加帖子的LIKE\u计数。你知道吗

使用下面的命令,我无法将postid发布到“/like”函数,也无法从中获取回调。终端显示类型错误:

like() missing 1 required positional argument: 'postid'.

你知道吗喜欢.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>

你知道吗视图.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)

Tags: textidurlnewdoc地址typecount