视图 def moviecol_add(): mid = request.args.get("mid", "") uid = request.args.get("uid", "") moviecol = Moviecol.query.filter_by(movie_id=int(mid), user_id=int(uid)).count() if moviecol == 1: data = dict(ok=0) if moviecol == 0: moviecol = Moviecol(movie_id=int(mid), user_id=int(uid)) db.session.add(moviecol) db.session.commit() data = dict(ok=1) import json return json.dumps(data) 模板 <script> $(document).ready(function () { $("#btn-col").click(function () { var mid = {{ movie.id }}; var uid = {{ session['user_id'] }}; $.ajax({ url: "{{ url_for('home.moviecol_add') }}", type: "GET", data: "mid=" + mid + "&uid=" + uid, datatype: "json", success: function (res) { if (res.ok == 1) { $('#show_col_msg').empty(); $("#show_col_msg").append("收藏成功!"); } else { $("#show_col_msg").empty(); $("#show_col_msg").append("已经收藏!"); } } }) }); });