flask ajax回调(flask 12)

2019-08-13 14:40:06 浏览数 (1)

jquery.ajax()函数参数: url: type:GET data datatype complete:请求完成时回调函数 success:请求成功 error:请求失败

返回的三种ajax数据

return render_tempalte('xx.html') return jsonify(xxx) return '',204

例子

from flask import Flask,make_response,json from jinja2.utils import generate_lorem_ipsum

app = Flask(name)

@app.route('/post') def show_post(): post_body = generate_lorem_ipsum(n=2) return ''' <h1>A very long post</h1> <div class="body">%s</div> <button id="load">Load More</button> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script type="text/javascript">

('#load').click(function() {

('.body').append(data); } }) }) }) </script>''' % post_body

@app.route('/more') def load_post(): return generate_lorem_ipsum(n=1)

if name == 'main': app.run( host='0.0.0.0', port=8888, debug=True )

0 人点赞