当前位置:   article > 正文

Flask之ajax操作示例_flask 处理 ajex

flask 处理 ajex

`文末包含源码`

python3.6,flask使用简例,

 目录结构

index2.html 

  1. <html><head>
  2. <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  3. <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  4. <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  5. <script type=text/javascript src="{{
  6. url_for('static', filename='jquery.js') }}"></script>
  7. <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.js"></script>
  8. </head>
  9. <body>
  10. <script type=text/javascript>
  11. $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
  12. </script>
  13. <script type=text/javascript>
  14. $(function() {
  15. $('a#calculate').bind('click', function() {
  16. $.getJSON($SCRIPT_ROOT + '/_add_numbers', {
  17. a: $('input[name="a"]').val(),
  18. b: $('input[name="b"]').val()
  19. }, function(data) {
  20. $("#result").text(data.result);
  21. });
  22. return false;
  23. });
  24. });
  25. </script>
  26. <h1>jQuery Example</h1>
  27. <p><input type=text size=5 name=a> +
  28. <input type=text size=5 name=b> =
  29. <span id=result>?</span>
  30. <p><a href=# id=calculate>calculate server side</a>
  31. </body>

myserver2.py

  1. # coding:utf-8
  2. from flask import *
  3. from flask import request
  4. app = Flask(__name__)
  5. @app.route('/_add_numbers')
  6. def add_numbers():
  7. a = request.args.get('a', 0, type=int)
  8. b = request.args.get('b', 0, type=int)
  9. return jsonify(result=a + b)
  10. @app.route('/')
  11. def index():
  12. html=render_template('index2.html')
  13. return html
  14. @app.route('/test_post', methods=['GET', 'POST'])
  15. def test_post():
  16. return '{"name":"zhangsan"}'
  17. if __name__=='__main__':
  18. app.run(debug=True,host="0.0.0.0")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/367802
推荐阅读
  

闽ICP备14008679号