1

I am not able to retrieve data in datatable using datatable.js. The Json response is a string but in the output I get every character of the string in each row rather that jst two entries.

Please help. Thanks in advance

function fillGrid() {

    $.ajax({
        type: 'POST',
        url: 'BehindCode/client.aspx/fillgrid',

        cache: false,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        beforeSend: function () {

            $('#gridLoadingDiv').attr('style', 'display:block');
        },
        complete: function () {

            $('#gridLoadingDiv').attr('style', 'display:none');
        },
        data: "{}",

        success: function (data) {
            data = data.d;
            alert(data);

            $("#clientTable").DataTable({
                "searching": true,
                    "processing": true,
                    "data": data,

                    "columns": [{
                    "title": "CLIENT NAME"
                }]
            });
        }
    });
}

C# code..from where data is being retreived..

[WebMethod]

    public static string fillgrid()
    {
        BehindCode_client client = new BehindCode_client();

        string strfetch = "SELECT CLIENT_NAME FROM k_client_master";

        string aadata = "";

        client.ds = client.DEngine.GetDataSet(strfetch, "Data", client.conn);

        if (client.ds != null && client.ds.Tables[0].Rows.Count > 0)
        {
             aadata = JsonConvert.SerializeObject(client.ds);
         //  aadata = "{'draw':1, 'recordsTotal':2, 'recordsFiltered':2, 'data':[{'CLIENT_NAME': 'Pyrotech'},{'CLIENT_NAME':'Workspace'}]}";  // tried this also
         //  aadata = "{'data':[{'CLIENT_NAME': 'Pyrotech'}, {'CLIENT_NAME':'Workspace'}]}";   // tried this as well
        }
            return (aadata.Replace("'","\""));
    }
}

JSON Response as in developer tools JSON Response which i got in designer view


Output in datatable Output Table.. It is not showing "pyrotech' and "workspace" but evry character in every row.


enteries in database which are retreived

The enteries in the database from where the datatable is to be filled.

1 Answer 1

1

I'm not familiar with C# but on the JavaScript part you're not decoding the JSON data.

Try this:

success: function (data) {
    data = JSON.parse(data.d).Data;
    alert(data);

    $("#clientTable").DataTable({
        "searching": true,
        "processing": true,
        "data": data,

        "columns": [
            {data: "CLIENT_NAME"}
        ]
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

tried this.. it alerts "object Object" and dataTable shows "No data available in table"
Answer updated, try again. Your data array is contained in the Data attribute. This call was missing.
alerts two objects " object Object, object Object" and another error.."DataTables warning: table id=clientTable - Requested unknown parameter '0' for row 0. For more information about this error, please see datatables.net/tn/4" and the table is empty.
Your datatable was misconfigured too. I updated my answer. See datatables.net/manual/data for configuration examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.