如何从烧瓶显示json数据使用agax将数据输出到数据表

时间:2020-02-22 20:31:56

标签: html json rest flask datatables

这是我的HTML和服务器代码:

<table id="example" class="display" style="width:100%">
    <!-- 3 headers-->
    <thead>
        <tr>
            <th>class</th>
            <th>Email</th>
            <th>Student_id</th>
            <th>Username</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <th>Class</th>
            <th>Email</th>
            <th>Student_id</th>
            <th>Username</th>
        </tr>
    </tfoot>
</table>
<script>
function setupData() {
    $(document).ready(function () {
        $('#example').DataTable({
             "processing": true,
             "ajax": {
                 "type" :"GET",
                 "url":"/students",

                 "dataSrc": function(json){
                      var obj = (json);
                      console.log(obj);
                      return obj;
                    }

                 },
            "columns": [
                { data:"user_class"},
                {data: "user_email"},
    { data:"user_id"},
                { data:"user_username"}
              ]
        });
    });
}
$( window ).on( "load", setupData );
</script>

</body>
</html>

@app.route('/students', methods=['GET'])
def student_get():
    cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
    cursor.execute('SELECT * FROM students')
    details = cursor.fetchall()


    details = jsonify(details)
    # details contains json array
    return render_template('students.html', details=details)

我想在表格中显示数据,但我不断得到

无可用数据

基于我的控制台,我有6个对象。不好意思,这是我第一次使用Flask和jQuery。

谢谢您的帮助。

0 个答案:

没有答案