【发布时间】:2018-04-17 17:29:25
【问题描述】:
在 POST 请求后,我无法让 Flask 向 HTML 页面添加文本。 POST 返回一个 200 代码,表明它已通过,单击按钮时我在终端中看到它。此外,我看到单击按钮时打印了 request.data,因此似乎 python 代码输入了“POST”请求的 if 语句,但它从不使用结果文本呈现模板。有什么想法吗?
HTML:
<button onclick="do_ajax()">Do Ajax</button>
<p>{{result}}</p>
JavaScript:
function do_ajax() {
var req = new XMLHttpRequest();
req.open("POST", "/index/", true);
var blob = new Blob(['abc123'], {type: 'text/plain'});
req.send(blob);
}
烧瓶/Python:
@app.route('/<string:index>/', methods=['GET','POST'])
def my_form_post(index):
if request.method == 'POST':
print request.data
return render_template('%s.html' % index, result = "It's working!")
elif request.method == 'GET':
return render_template('%s.html' % index)
【问题讨论】:
-
您应该take a look here,不是直接回答您的问题,而是应该帮助您了解问题出在哪里。
标签: python html css ajax flask