我正在做一个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
我的猜测是你的/myurl
路线没有被定义为接受POST
请求,而你的/<a>/
路线是,所以Werkzeug选择/<a>/
.
此处说明以斜杠结尾的路由的行为.默认情况下,调用使用尾部斜杠定义的路径而不使用该斜杠会触发重定向到URL的尾部斜杠版本.这当然不适合POST
请求,所以你得到了FormDataRoutingRedirect
例外.
我怀疑如果你发送你的POST
请求/myurl/
然后你的/<a>/
路线会被调用很好,但显然这不是你想要的.
我认为您缺少的是接受POST
请求/myurl
,您可以执行以下操作:
@app.route('/myurl', methods = ['GET', 'POST']) def my_func():