kri*_*ish 4 javascript python ajax json flask
我正在尝试通过ajax调用将数组发送到flask。但是它不起作用。
Java脚本
<script type="text/javascript">
function fillChart()
{
var nids = document.getElementById("nodes-select").value;
var cfilter = document.getElementById("filter-select").value;
var chkd = document.getElementById("further-select");
var cids = [];
for (var i=0;i<chkd.length;i++)
{
if(chkd[i].selected)
{
cids.push(chkd[i].value);
}
}
alert(cids);
$.post("/pie",{"node_id":nids,"col_select":cfilter,"col_filter":cids},function(data,status)
{
var tmp = data;
console.log(data.otstr);
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
服务器代码
@app.route('/pie',methods=['POST'])
def pie():
tmp1 = request.form.get('node_id')
tmp2 = request.form.get('col_select')
tmp3 = request.form.get('col_filter[]')
return jsonify(otstr=[tmp1,tmp2,tmp3])
Run Code Online (Sandbox Code Playgroud)
这里tmp1和tmp2只是字符串,而tmp3是字符串数组.console.log(data.otstr)打印正确的tmp1,tmp2值,但是当涉及到tmp3时,因为它是一个数组,所以它仅打印第一个元素。
您需要检索col_filter为列表:
tmp3 = request.form.getlist('col_filter[]')
Run Code Online (Sandbox Code Playgroud)