我试图从烧瓶中的数据表中获取行信息。该数组在JQuery端显示数据,但在flask中访问时为空。如何传递数组以便可以使用python访问它?
烧瓶
@api.route('/apply_billing')
def apply_billing():
bill_month = request.args.get('bill_month', 0)
markup = request.args.get('markup', 0, type=float)
billed = request.args.get('billed', 0, type=float)
comment = request.args.get('comment', 0, type=str)
tickets = request.args.getlist('tickets')
print(tickets)
return jsonify(result=str(bill_month) + str(markup))
JQuery
<script type=text/javascript>
$(function() {
$('#submit').bind('click', function() {
var ticket_array = [];
$('#billinglist').DataTable().rows('.selected').every(function(rowIdx){
ticket_array.push($('#billinglist').DataTable().row(rowIdx).data())
});
var data = {
"bill_month": $('input[name="month"]').val(),
"markup": $('input[name="markup"]').val(),
"billed": $('input[name="billed"]').val(),
"comment": $('input[name="comment"]').val(),
"tickets": ticket_array
}
console.log(data)
$.ajax({
url: "{{ url_for('api.apply_billing') }}",
data: data,
success: function(response) {
alert(response.result);
$('#billinglist').DataTable().ajax.reload();
}
});
return false;
});
});
</script>
答案 0 :(得分:0)
我能够使用https://stackoverflow.com/users/100297/martijn-pieters上的martijn-pieters的答案来解决此问题。
烧瓶:
@api.route('/apply_billing', methods=["POST"])
def apply_billing():
data = request.get_json()
print(data["tickets"])
return jsonify("test")
JQuery
<script type=text/javascript>
$(function() {
$('#submit').bind('click', function() {
var ticket_array = [];
$('#billinglist').DataTable().rows('.selected').every(function(rowIdx){
ticket_array.push($('#billinglist').DataTable().row(rowIdx).data())
});
var data = {
bill_month: $('input[name="month"]').val(),
markup: $('input[name="markup"]').val(),
billed: $('input[name="billed"]').val(),
comment: $('input[name="comment"]').val(),
tickets: ticket_array
}
console.log(data)
$.ajax({
url: "{{ url_for('api.apply_billing') }}",
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
success: function(response) {
alert(response.result);
$('#billinglist').DataTable().ajax.reload();
},
error: function(){
alert("No Bueno!!")
}
});
return false;
});
});
</script>