2015-06-24 18 views
1

我试图从window.history.pushState所做的更改放到烧瓶服务器上。例如,如果我这样做:烧瓶接收来自pushstate的url更改

window.history.pushState("object or string", "Title", "?firstchange=done"); 

如何在烧瓶中获得“完成”字符串?我在客户端路由下有一个模板,并且我想要一个函数在可用时获取firstchange查询字符串。

@app.route("/client") 
def initial_template(): 
    return render_template('client.html') 

@app.route("/client") 
def get_change(): 
    print request.args.get('firstchange') 

,当我的网址更改为http://127.0.0.1:5000/client?firstchange=done后调用这个在其它函数的返回是“无”,而不是“完成”。

编辑:

你如何使用

window.location.href('127.0.0.1:5000/client?firstchange=done') 

用同样的方法烧瓶我仍然得到“没有”的回报做到这一点。我能否在其他方法中使用get_change方法来确定查询字符串值?

+2

推送状态发生在客户端,Flask只是服务器端。你必须做'window.location.href('/ client?firstchange = done')'或使用AJAX。 – nathancahill

+0

请参阅编辑新问题。 – shell

回答

0

我不知道你怎么能在相同的路线下注册两种方法,但我不认为这是一个好主意,你的烧瓶服务器将如何表现。 所以,请改变你的路线之一。 我这里还有两种方式这样做:

1)

@app.route("/client") 
def initial_template(): 
    return render_template('client.html') 

@app.route("/client_state") 
def get_change(): 
    print request.args.get('firstchange') 

在这种情况下,你的JavaScript重定向代码将是这样的: window.location.href('127.0.0.1:5000/client_state ?firstchange =完成“)

2)

@app.route("/client") 
def initial_template(): 
    return render_template('client.html') 

@app.route("/client/<state>") 
def get_change(state): 
    print state 

在这种情况下你的JavaScript重定向代码如下所示: window.location.href('127.0.0.1:5000/client/done')

看看this documentation有其他问题。