使用jQuery数据表时出错

时间:2014-12-15 12:57:28

标签: jquery jquery-datatables

我正在使用jquery数据表来列出结果。 datatable正在进行ajax调用以获取数据。我可以从控制台日志中看到正确检索数据。但它无法正确呈现。我收到错误

  

DataTables warning(table id ='datatable'):从第1行的数据源请求未知参数'2'。

UI中有3列。 “Country”列填充“U”,“Name”列填充“S”。它应该是国家列中的“US”,第二列中的银行名称和第3列中的地址。不确定我错过了什么。非常感谢任何帮助。

<table id="datatable" class="display dataTable">
    <thead>
        <th>Country</th>                            
        <th>Bank</th>
        <th>Address</th>
    </thead>
    <tbody>
    </tbody>
</table>
function searchBanks() {
    initSearchTable();
    populateBanks();
}

var oBankTable;

function initSearchTable() {
    oBankTable = $('#datatable').dataTable({
        "sAjaxDataProp": "bank",
        "bPaginate": false,
        "bFilter": false,
        "bLengthChange": false,
        "bInfo": false,
        "bAutoWidth": false,
        "bJQueryUI": true,
        "bDeferRender": true,
        "aoColumns": [
            { "mData": "country", sDefaultContent: "" },
            { "mData": "name" , sDefaultContent: ""},
            { "mData": "address" , sDefaultContent: ""}
        ]        
    });
}

function populateBanks() {
    var endClientRequest = $.ajax({
        url: "../api/banks",
        type: "GET",
        dataType: "json"
    });
    endClientRequest.done(function (response) {
        oBankTable.fnClearTable();
        for(var i=0; i< response.bank.length ; i++) {
            var tmp_bank = response.bank[i];
            console.log(tmp_bank.country + "**" + tmp_bank.name + "**" +  tmp_bank.city + "**" +  tmp_bank.address + "**" +  tmp_bank.swiftBIC);
            oBankTable.fnAddData(tmp_bank.country, tmp_bank.name, tmp_bank.city, tmp_bank.address, tmp_bank.swiftBIC);
        }      

        return false;
    });

    endClientRequest.fail(function (jqXHR, textStatus) {
        console.log("request failed");
        show_error_dialog("Unable to connect to the server at present to retrieve the detail.");
        return false;
    });
    return false;
}

JSON响应:

{
    "banks": [{
        "country": "US",
        "name": "Bank1",
        "address": "Address 1",
    }, {
        "country": "US",
        "name": "Banks2",
        "address": "Address 2",
    }
]}

1 个答案:

答案 0 :(得分:1)

您可能需要删除地址标记后面的“,”。

{
    "banks": [{
        "country": "US",
        "name": "Bank1",
        "address": "Address 1"
    }, {
        "country": "US",
        "name": "Banks2",
        "address": "Address 2"
    }
]}

您可以在http://jsonlint.com/

中验证json