我想通過post請求使用ajax將一些數據從html傳遞給python。但請求的論據是空的。使用Ajax將數據傳入燒瓶
一個簡單的端點,我試圖打印所有以請求的形式出現的參數。
@app.route("/enter", methods=["POST", "GET"])
def enter_data():
if request.method == "GET":
return render_template('index.html')
else:
print("post")
print(request.args)
return render_template('index.html')
調試輸出
post
ImmutableMultiDict([])
127.0.0.1 - - [22/Sep/2016 00:40:12] "POST /enter HTTP/1.1" 200 -
的AJAX功能是如下
$(function() {
$('.button').click(function() {
var user = $('#userinput').val();
console.log(user);
$.ajax({
url: '/enter',
data: JSON.stringify({'user': $('#userinput').val()}),
type: 'POST',
success: on_request_success,
error: on_request_error,
})
});
});
值獲取JS控制檯打印。但它在Python方面不存在。
HTML代碼
<body>
<h3>The Chef Bucket</h3>
Enter food you like to order.
<input type="text" name="user_data" id="userinput" />
<input type="button" value="Submit" class="button">
<div id="userinputdata">
{% if not entered %}
<h4></h4>
{% else %}
<h4>{{ entered }}</h4>
{% endif %}
</div>
</body>
on_request_success函數是在哪裏? – madalinivascu
它不會打印任何內容,因爲'POST/enter'中沒有參數。如果你輸入'POST/enter?foo = bar',它會打印什麼? – alexpeits