【发布时间】:2022-01-25 01:19:26
【问题描述】:
我是初学者。 我用的是烧瓶和pymongo。 如果你按下按钮,它就是“喜欢”。应该是+1,但是底部有个key错误。
我的python路由代码:
@app.route('/api/like', methods=['POST'])
def like_movie():
title_receive = request.form['title_give']
movie = db.toytoy.find_one({'title': title_receive})
current_like = movie['like']
new_like = current_like + 1
db.toytoy.update_one({'title': title_receive}, {'$set': {'like': new_like}})
return jsonify({'msg': 'like!'})
这就是我从 JS 发布的方式
function like_movie(title) {
$.ajax({
type: 'POST',
url: '/api/like',
data: {title_give: title},
success: function (response) {
console.log(response)
alert(response['msg']);
window.location.reload();
}
});
}
我得到如下异常:
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'title_give'
我想要的是“like_btn”。如果你按下按钮,它就会变成 +1。
【问题讨论】: