html – how to display json data from flask get output to datatables using agax-ThrowExceptions

Exception or error:

This is my HTML and server code:

<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)

I would like to have data displayed in the table but I keep getting

no data available

Based on my console I have 6 objects. Excuse my poor skills, this is my first time using Flask and jQuery.

Thank you for your help.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *