• Flask与Ajax


      这篇短文使用jquery。

      Flask提供一个很简单的方法来处理Ajax请求——在视图函数中用request的属性is_xhr来判断,如果是true则是异步请求。

      Jquery的$.getJSON()方法会主动向服务端发出ajax请求(不知这个理解是否正确?),服务端响应后调用$.getJSON的回调函数。在回调函数中就可以操作Html页面上的元素了。

    客户端:ajax.html

    <!doctype html>
    <html lang="en">
      <head>
        <meta charset="UTF-8"/>
        <title>Document</title>
        <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
      </head>
      <body>
        <h1>Ajax</h1>
        <div id="test">Show data count:</div>
        <script src="//cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
        <script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script>
         $(document).ready(function(){
           $.getJSON('/ajax',function(data){
         $('#test').append('<span class="badge">'+data.count+'</span>');
           });
         });
        </script>
      </body>
    </html>

    服务端:

    @app.route('/ajax')
    def y():
       if request.is_xhr:
           y = Area.query.all()
           return jsonify({'count':len(y)})
       return render_template('ajax.html')

    --End--

  • 相关阅读:
    Construction构造函数
    映射验证
    映射设置
    条件映射
    映射前和映射后的操作
    AutoMapper 5.0-升级指南
    Bootstrap Tree View
    MiniProfiler使用笔记
    关于添加数据自定义编号格式问题
    【Postgresql】数据库函数
  • 原文地址:https://www.cnblogs.com/ibgo/p/4640773.html
Copyright © 2020-2023  润新知