当前位置:   article > 正文

使用ajax完成python flask前端与后台数据的交互_flask和bootstrap交互ajax

flask和bootstrap交互ajax

python端:

  1. from flask import Flask, render_template
  2. import json
  3. import os
  4. app = Flask(__name__)
  5. testInfo = {}
  6. @app.route('/test_post/nn', methods=['GET', 'POST']) # 路由
  7. def test_post():
  8. testInfo['name'] = 'xiaoliao'
  9. testInfo['age'] = '28'
  10. return json.dumps(testInfo)
  11. @app.route('/')
  12. def hello_world():
  13. return 'Hello World!'
  14. @app.route('/index')
  15. def index():
  16. return render_template('index.html')
  17. if __name__=="__main__":
  18. app.run()

JS端:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>echarts</title>
  8. </head>
  9. <body>
  10. <p>数据展示:
  11. <input type="text" id ="NAME">
  12. <input type="text" id ="AGE"></p>
  13. <p>
  14. <button id = "btn">点击获取</button>
  15. <input type="text" id ="NAME2">
  16. <input type="text" id ="AGE2"></p>
  17. <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.js"></script>
  18. <script>
  19. $.ajax({
  20. url: "test_post/nn",
  21. type: "POST",
  22. dataType: "json",
  23. success: function (data) {
  24. // alert(data.name + ":" + data.age);
  25. $("#NAME").val(data.name);
  26. $("#AGE").val(data.age);
  27. console.log(data)}
  28. })
  29. </script>
  30. <script>
  31. $(function() {
  32. // body...
  33. $("#btn").click(function(){
  34. $.ajax({
  35. url: "test_post/nn",
  36. type: "POST",
  37. dataType: "json",
  38. success: function (data) {
  39. // alert(data.name + ":" + data.age);
  40. $("#NAME2").val(data.name);
  41. $("#AGE2").val(data.age);
  42. console.log(data)}
  43. })
  44. })
  45. })
  46. </script>
  47. </body>
  48. </html>

参考链接:

https://www.cnblogs.com/showersun/p/3745355.html

https://blog.csdn.net/we1583004we/article/details/81512893

http://www.w3school.com.cn/jquery/ajax_post.asp

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

闽ICP备14008679号