【发布时间】:2019-02-17 13:17:48
【问题描述】:
我知道您可以使用 jquery 中的 $.ajax() 之类的调用轻松地将表单数据发送到烧瓶,但我需要的是一种使用元素(例如 <p> 标记)将数据发送到烧瓶服务器的方法。
例如: 测试.html
<p data-rep='testentry' class='qtemp'>Entry00</p>
<p data-rep='testentry01' class='qtemp'>Entry01</p>
index.js
$(document).ready(function(){
$('.qtemp').on('click',function(){
var layout = $(this).data('rep');
$.ajax({
url: 'workstation',
type: 'POST',
data: layout
});
});
});
main.py
@app.route('/workstation',methods=['GET','POST'])
def workstation(data):
layout = data
return render_template('station.html',target=layout)
运行这个应用程序:
- 不渲染模板
station.html - 不打印包含已发送数据的
layout变量(我添加了一个打印语句但它不起作用,甚至尝试将其写入文件)
我尝试过的:
- 在
index.js中,将data: layout替换为data: JSON.stringify(layout),然后在.main.py中,将layout = data替换为layout = request.args.get('data')。
不用说,这一切都行不通
注意:不能使用 html 表单
【问题讨论】:
-
您的 Ajax 请求中没有
success回调,因此响应会被忽略
标签: python jquery python-3.x flask