我正在执行一个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=''):

最后一个会挡道吗?或者什么?烧瓶版本为0.10.1

最佳答案

我的猜测是您的/myurl路由没有定义为接受POST请求,而您的/<a>/路由是,所以Werkzeug选择了/<a>/
以斜线结束的路由的行为解释为here。默认情况下,调用一个没有斜杠的尾随斜杠定义的路由会触发对URL尾随斜杠版本的重定向。当然,这对于POST请求不起作用,因此您会得到FormDataRoutingRedirect异常。
我怀疑,如果您将您的POST请求发送到/myurl/中,那么您的/<a>/路由将被调用,但显然这不是您想要的。
我认为您缺少的是接受POST上的请求,您可以执行以下操作:

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

10-01 18:29
查看更多