【问题标题】:FormDataRoutingRedirect exception from a URL without trainling slashFormData 路由从没有斜杠的 URL 重定向异常
【发布时间】:2013-12-16 02:23:25
【问题描述】:

我正在执行 ajax POST 请求,但遇到了这个异常:

[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]     self.raise_routing_exception(req)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]   File "/usr/lib/python2.6/site-packages/flask/app.py", line 1439, in raise_routing_exception
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100]     raise FormDataRoutingRedirect(request)
[Fri Nov 29 20:48:55 2013] [error] [client 192.168.25.100] FormDataRoutingRedirect: A request was sent to this URL (http://example.com/myurl) but a redirect was issued automatically by the routing system to "http://example.com/myurl/".  The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one.  Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.

路由定义是这样的:

@app.route('/myurl')
def my_func():

我可以在 Firebug 中看到发送的请求没有尾部斜杠:

http://example.com/myurl
Content-Type    application/x-www-form-urlencoded; charset=UTF-8

我在另一个模块中有这个:

@app.route('/')
@app.route('/<a>/')
@app.route('/<a>/<b>')
def index(a='', b=''):

这最后一个会妨碍吗?要不然是啥? Flask 版本为 0.10.1

【问题讨论】:

    标签: python url flask python-2.6


    【解决方案1】:

    我的猜测是您的 /myurl 路由未定义为接受 POST 请求,而您的 /&lt;a&gt;/ 路由是,因此 Werkzeug 选择了 /&lt;a&gt;/

    here 解释了以斜线结尾的路由的行为。默认情况下,调用使用尾部斜杠定义的路由而没有该斜杠会触发重定向到 URL 的尾部斜杠版本。这当然不适用于POST 请求,因此您会得到FormDataRoutingRedirect 异常。

    我怀疑如果您将POST 请求发送到/myurl/,那么您的/&lt;a&gt;/ 路由将被很好地调用,但显然这不是您想要的。

    我认为您缺少的是在 /myurl 上接受 POST 请求,您可以执行以下操作:

    @app.route('/myurl', methods = ['GET', 'POST'])
    def my_func():
    

    【讨论】:

    • 我的猜测是您的 /myurl 路由未定义为接受 POST 请求,而您的 // 路由是。两者都没有被定义为接受POST 或任何其他方法,但是当我将POST 方法添加到/myurl 时,它起作用了。
    • 啊,好吧。所以重定向表单错误发生在 Flask 决定不匹配您的请求类型之前。
    猜你喜欢
    • 1970-01-01
    • 2013-02-17
    • 2012-02-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多