javascript - 从 Vue.js 向烧瓶发送 POST 请求
问题描述
我想从 Vue 组件向 Flask 后端发送一个 POST 请求。我尝试了以下代码:
在 script.js 中:
finishGame: function () {
this.gameStarted = false;
this.$http.post("http://127.0.0.1:5000/", "hello")
.then(function () {
alert("POST")
});
},
在 app.py 中:
from flask import Flask, render_template
from flask_cors import CORS
from model.words import Words
# Set up custom delimiters to avoid Vue.js conflict.
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='(%',
block_end_string='%)',
variable_start_string='((',
variable_end_string='))',
comment_start_string='(#',
comment_end_string='#)',
))
app = CustomFlask(__name__)
CORS(app)
word_gen = Words("model/word_lists/onegin.csv")
@app.route('/', methods=['POST'])
def save_game_results():
print("Game res!")
return render_template('index.html', words=word_gen.get_random(500), words_num=word_gen.size())
@app.route('/')
def play_hat():
return render_template('index.html', words=word_gen.get_random(500), words_num=word_gen.size())
该脚本包含更多代码,但该finishGame
函数肯定会运行,并且只有盯着的部分this.$http
被忽略。该代码不会产生任何错误,但save_game_results()
在被调用时永远不会finishGame()
被调用。
我已经尝试过:
- 替换
"http://127.0.0.1:5000/"
为"/"
. "http://127.0.0.1:5000/"
用"http://127.0.0.1:5000/go"
和替换@app.route('/', methods=['POST'])
_@app.route('/go', methods=['POST'])
它没有奏效。
解决方案
推荐阅读
- excel - 使用宏用时间戳填充列
- vue.js - vuejs 递归单文件组件
- android - 为什么我在 Android Studio 中收到“未解析的引用:绑定”错误?
- c# - WPF QuickConverter:布尔值且不起作用
- javascript - Puppeteer 无法发布
- java - 如何将一棵树转换为另一棵树?
- sql-server - 使用 Lead/Lag 对 SQL 查询中的相关记录进行分组
- cad - 如何使用 OpenCascade 在 3d 中可视化 .stp 文件?
- redis - 在 Redis 中选举新的 master 之前数据会发生什么?
- java - 索引越界Java合并排序?