没有训练斜杠的URL中的FormDataRoutingRedirect异常

 借钱买黄瓜 发布于 2023-02-13 18:28

我正在做一个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('//')
@app.route('//')
def index(a='', b=''):

这最后一个会妨碍吗?或者是什么?Flask版本是0.10.1

1 个回答
  • 我的猜测是你的/myurl路线没有被定义为接受POST请求,而你的/<a>/路线是,所以Werkzeug选择/<a>/.

    此处说明以斜杠结尾的路由的行为.默认情况下,调用使用尾部斜杠定义的路径而不使用该斜杠会触发重定向到URL的尾部斜杠版本.这当然不适合POST请求,所以你得到了FormDataRoutingRedirect例外.

    我怀疑如果你发送你的POST请求/myurl/然后你的/<a>/路线会被调用很好,但显然这不是你想要的.

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

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

    2023-02-13 18:31 回答
撰写答案
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有