我正在编写一个小应用程序。这是代码片段。
Javascript:
<script type="text/javascript">
function filldata()
{
var sample = document.getElementById("filter-select").value;
jQuery.support.cors = true;
$.post("/test",{"filter_type":sample},function(data,status)
{
alert(data);
});
}
</script>
flask 代码:
@app.route('/test', methods=['POST'])
def test(str):
return str
它给我 500 内部错误
。
当我在 Debug模式下运行它时,它说:
test() takes exactly one argument(zero given)
最佳答案
您的代码:
@app.route('/test',methods=['POST'])
def test(str):
return str
期望输入中有一个名为 str 的变量。 使用 flask,在定义路由时,函数的参数表示 url 变量:
# the url have a parameter
@app.route('/user/<id>',methods=['GET'])
def test(id): # we get it here
return user.fromId(id)
要检索查询字符串,您可以使用 request.args
。并获取正文 request.data
或 request.json
如果您发送的是 json。
from flask import request
@app.route('/test',methods=['POST'])
def test():
return request.data
关于javascript - throw 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33077276/