Pyton flask ajax,python-flask example for ajax

I think what you are missing is that each time the client requests a number increase there is an independent request. Your inc_number handler would be coded as follows:

@app.route('/_inc_number')

def inc_number():

n = request.args.get('n', 0, type=int)

n = n + 1

return n

Then from the jQuery side you have to invoke an independent Ajax request each time you want to increase the number.

Note that with this type of solution the jQuery app is the one that keeps track of the current value of the counter, and it has to send it to Flask with each request. Another possibility would be to have the Flask side remember the number in a user session. For that type of solution your Flask app would have two view functions:

from flask import session

# you need to set a SECRET_KEY in configuration, used to sign the user session

@app.route('/set_number')

def set_number():

# store n in the user session

session['n'] = request.args.get('n', 0, type=int)

@app.route('/inc_number')

def inc_number():

session['n'] = session['n'] + 1

return session['n']

With this solution now jQuery can set the number and not have to send it every time when it invokes inc_number.

I hope this helps.

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值