简体   繁体   English

触发瓶功能,无需重新加载页面

[英]Trigger bottle function without reloading page

my bottle app.py looks something like: 我的瓶子app.py看起来像:

@route('/submit/<var>')
def submit(var):
  # do action
  redirect("/")

and i have a simple html button which redirects to the required page when clicked through 而且我有一个简单的html按钮,单击该按钮可重定向到所需的页面

onclick="window.location.href='/submit/value'"

but this causes the whole page to effectively refresh by going to the link and then coming back straight away, putting the user to the top of the page and so on. 但这会导致整个页面有效地刷新,方法是转到链接,然后立即返回,将用户置于页面顶部,依此类推。

How can i get it to do the bottle backend without refreshing? 我怎样才能不刷新就做瓶子后端?

In your template: 在您的模板中:

<button type="button" onclick="onSubmit('Value')">Sumbit!</button>>
<p id="response"></p>
<script>
function onSubmit(value) {
    var myRequest = new Request('/submit/' + value);
    var myInit = { method: 'PUT' };
    fetch(myRequest, myInit).then(function(response) {
        return $("#response").text(response);
    });
}
</script>

In the python side 在python方面

@put("/submit/<val>")
def submit(val):
    return "Yay it worked!"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM