【问题标题】:Post throwing 500 error投掷 500 错误
【发布时间】:2015-10-12 09:09:40
【问题描述】:

我正在编写一个小应用程序。这是代码sn-p。

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>

烧瓶代码:

@app.route('/test', methods=['POST'])
def test(str):
    return str

它给了我500 internal error
当我在调试模式下运行它时,它说:

test() takes exactly one argument(zero given)

【问题讨论】:

  • 我建议您在这种情况下使用 fiddler,这样您就可以在回复中看到错误。您正在提交名称为 filter_type 的字段,但您的服务器端是否有该名称的字段?

标签: javascript python ajax flask


【解决方案1】:

您的代码:

@app.route('/test',methods=['POST'])
def test(str):
    return str

在输入中期望一个名为 str 的变量。 使用烧瓶,在定义路由时,函数的参数代表 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。如果您发送 json,则获取正文 request.datarequest.json

from flask import request

@app.route('/test',methods=['POST'])
def test():
    return request.data

【讨论】:

  • 非常感谢 Cybril :)
【解决方案2】:

您需要从 Flask 的 request 对象中解析参数

from flask import request

@app.route('/test',methods=['POST'])
def test():
    return request.form.get('filter_type')

更多信息请见quickstart

【讨论】:

  • 非常感谢彼得 :)
【解决方案3】:

在您的 Flask 代码 sn-p 中,您添加了一个参数,但仅当您将 URL 更改为 /test/&lt;str:str&gt; 时才会发送参数。由于是POST请求,所以可以使用request.json访问数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多