所以我正在嘗試使用 Ajax 和 Flask 將一些 jquery 數(shù)據(jù)從前端發(fā)送到后端,但我不斷收到此錯(cuò)誤,但我的所有其他代碼都運(yùn)行良好我嘗試使用 type: 'POST' 和 $.post 來嘗試發(fā)送數(shù)據(jù),但我運(yùn)氣不佳。我也嘗試使用 request.form 和 request.get_json(),但仍然沒有運(yùn)氣。我嘗試在郵遞員中運(yùn)行一個(gè)快速測試,它告訴我。我正在嘗試運(yùn)行 POST 請求。來自郵遞員的代碼<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><title>400 Bad Request</title><h1>Bad Request</h1><p>The CSRF token is missing.</p>蟒蛇燒瓶#all other code is fine...@users.route('/api/get_msg') #Both users can communicatedef get_msg(): text = Messages.query.all() users = Users.query.all() return jsonify([ { "msg": text[m].message, "user": text[m].sender.username } for m in range(len(text))])@users.route('/api/send_msg', methods=['POST'])def send_msg(): data = request.get_json() from_user = current_user msg_text = data["msg_text"] newMsg = { "msg_text": msg_text } msg_log = Message(message=msg_text, sender=from_user) db.session.add(msg_log) db.session.commit() return jsonify({"result", "success"})我試圖讓后端將消息保存到數(shù)據(jù)庫,然后通過 get 請求將其發(fā)送回前端。就像聊天應(yīng)用程序一樣。我還試圖找出使用 Flask 和 jquery 創(chuàng)建即時(shí)聊天應(yīng)用程序的最佳方法。如果有人有一些建議。請告訴我。我已經(jīng)在使用 pusher 實(shí)時(shí)推送消息。
我如何用 Flask 和 jquery 解決這個(gè)問題?
慕碼人8056858
2021-06-28 22:43:40