我想将数据从JS发送到Python。对于这项工作,我使用这个脚本:
function get_data(table, values, name, keyword_column) {
alert(table)
alert("sending data")
$.ajax({
type: "POST",
url: "/data",
data: {
keyword: name,
table: table,
columns: JSON.stringify(values),
keyword_column: keyword_column
},
success: function(data)
{
return data;
},
error: function(xhr, ajaxOptions, thrownError){
alert(xhr.responseText);
}
});
//return false; // avoid actual submit
}
上述功能的示例输入:
get_data('string',['item1','item2','item3'], 'string', 'string');
和Python:
@app.route("/data", methods=['POST', 'GET'])
def get_data():
print('im here')
print(str(request.form))
table = request.form['table']
keyword = request.form['keyword']
columns = request.form['columns']
keyword_column = request.form['keyword_column']
print(table)
然而,当这样做时,我收到以下错误:
回溯(最近一次调用最后一次):
File "C:\Users\""\Anaconda\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\""\Anaconda\lib\site-packages\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "C:\Users\""\Anaconda\lib\site-packages\flask\app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "C:\Users\""\Anaconda\lib\site-packages\flask\app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "C:\Users\""\Anaconda\lib\site-packages\werkzeug\wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "C:\Users\""\Anaconda\lib\site-packages\werkzeug\test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'list' object is not callable
正如你所看到我打印了请求对象,这似乎是oke:
ImmutableMultiDict([('keyword', 'S-t'), ('table', 'gene'), ('columns', '["gene_id","name","aliases","location","description"]'), ('keyword_column', 'name')])
似乎JSON.stringify
做了它的工作,但是Flask仍然会出错。