【问题标题】:Datatable from server json object来自服务器 json 对象的数据表
【发布时间】:2018-05-20 01:06:55
【问题描述】:

我将表单数据作为 ajax 发布请求提交给后端 Flask 应用程序。后端进程使用数据查询数据库,然后将 json 对象返回给 Ajax。如何从这个 json 对象构造数据表?

我的 Ajax 发布请求:

$(document).ready(function() {
    $("#submit").click(function(event) {
        $.ajax({
            type: 'POST',
            url: '/process',
            data: $('#myform').serialize(),
            success: function(response) {
                //response looks like this: 
                //[{"country": "USA", "code": "1007-01" },{"country": "UK", "code": "1100-04" }]

            }
        });
        event.preventDefault();
    });
});

我的烧瓶应用程序

@app.route('/process', methods=["POST"])
def process():
    if request.method == 'POST':
       country = request.form['id'].encode("utf-8")
       #query database and store result in dict
       result = query_database(country)
       return jsonify(result)

【问题讨论】:

标签: javascript jquery ajax datatables


【解决方案1】:

使用 ajax 调用作为数据源初始化数据表

let datatable = $('#my-table').DataTable({
    ajax: {
        url: '/process',
        method: 'POST',
        data: {
            $('#myform').serialize()
        } 
    }
});

现在你只需要像这样格式化你的回复

{ 
    data:[/*your Data*/]
}

【讨论】:

    猜你喜欢
    • 2010-09-06
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    相关资源
    最近更新 更多