【问题标题】:Jquery datatables not displaying data in heroku python django?Jquery数据表没有在heroku python django中显示数据?
【发布时间】:2019-02-15 02:07:16
【问题描述】:

我正在尝试以 html 表单呈现 jquery 数据表。现在,由于某种奇怪的原因,我可以看到我可以从我的 API 接收数据,但数据表中没有显示任何内容。有趣的是,当我在本地机器上运行时,相同的代码可以完美运行。但是当我检查我的 heroku 日志中的错误时,我发现我收到了这个错误。 错误日志

(index):254 Uncaught TypeError: Cannot read property 'length' of undefined
    at Object.success ((index):254)
    at fire (jquery-1.12.4.js:3232)
    at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
    at done (jquery-1.12.4.js:9840)
    at XMLHttpRequest.callback (jquery-1.12.4.js:10311)

但在我的本地服务器中,一切正常。

  $(document).ready(function () {

            //We have to get all users in the system

            var jsonres=[]

            var orgid = $('#orgid').val();
            console.log('orgid',orgid)

            $.ajax({

                url: "/user/api/v1/userapi/",
                type: "get",




                success: function (json) {
                    console.log("json", json)


                    json = json.results;
                    for(var i=0;i<json.length;i++){
                        console.log('jsonnnn',json[i].orgid)

                        if(json[i].orgid==orgid){
                            jsonres.push(json[i])
                        }
                    }



                   console.log('jso',jsonres)
                    var data = jQuery.map(jsonres, function (el, i) {
                        /*  if(el.title.length>20){
                              el.title = el.title.substring(0,10) + '..........';
                          }*/

                        return [[el.userid, el.firstname, el.lastname, el.mobilenumber, el.email, el.isactive]];
                    });

                    $('#demotbl').DataTable({
                        "searching": true,
                        "bLengthChange": false,
                        "order": [[3, "desc"]],


                        "aaData": data,
                        "bPaginate": true,

                        "aoColumns": [
                            {"sTitle": "userid"},

                            {"sTitle": "firstname"},

                            {"sTitle": "lastname"},
                            {"sTitle": "mobilenumber"},
                            {"sTitle": "email"},
                            {"sTitle": "isactive"},

                        ]


                    })


                }
            })


        })


[
    {
        "userid": "test_001123456",
        "firstname": "Remy",
        "lastname": "Das",
        "email": "av20078@gmail.com",
        "mobilenumber": "082929992",
        "isactive": "yes",
        "orgid": "test_001"
    },
    {
        "userid": "test_0013456",
        "firstname": "s",
        "lastname": "sss",
        "email": "ABC@gmail.com",
        "mobilenumber": "786894651",
        "isactive": "yes",
        "orgid": "test_001"
    }
]

【问题讨论】:

  • 在你的代码中使用dataSrc:' ',

标签: python jquery django heroku datatables


【解决方案1】:

使用"dataSrc": "",您的问题就解决了。

问题是因为,当您使用服务器端 ajax 时,它期望数据变量名称为 jsonResponse 类似:

data: "[
    {
        "userid": "test_001123456",
        "firstname": "Remy",
        "lastname": "Das",
        "email": "av20078@gmail.com",
        "mobilenumber": "082929992",
        "isactive": "yes",
        "orgid": "test_001"
    },
    {
        "userid": "test_0013456",
        "firstname": "s",
        "lastname": "sss",
        "email": "ABC@gmail.com",
        "mobilenumber": "786894651",
        "isactive": "yes",
        "orgid": "test_001"
    }
]";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多