我正在尝试将数据发布到Flask应用程序。它接收POST请求,但我不确定如何请求数据。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
function Submit() {
var myData = "this is my data string"
$.post("/receivedata", function(myData , status){
alert(status);
});
}
在烧瓶中:
@application.route('/taskinputjs', methods=['GET', 'POST'])
@login_required
@roles_accepted('Admin')
def taskinputjs():
print "taskinputjs..."
if request.method == 'POST':
data = request.args.get('myData', None, type=str)
#data = request.form['myData']
return render_template('index.html')
它发布,但myData
如何正确请求数据?
答案 0 :(得分:6)
这可能是因为您实际上并未发送任何数据。它应该是这样的(jQuery docs):
<button onclick=submit()>Click me!</button>
<script>
function submit() {
var myData = "This is my data string."
$.post("/receivedata", {"myData": myData})
}
</script>
并在Flask app中:
application.route('/receivedata', methods=['POST'])
def receive_data():
print request.form['myData']
单击按钮时将在flask控制台中输出数据:
this is my data string