Get data Client Side using Ajax call

Get data Client Side using Ajax call

wathertonwatherton Posts: 33Questions: 6Answers: 0

Hi,

I'm struggling to load data from a ajax cal into a asp.net c# web method. My code is:

var oMessageDate;
var tblData;
$(document).ready(function () {

getData();

oMessageDate = $("#tblDataTable").DataTable({
    data: tblData
});

});

function getData() {
$.ajax({
type: "POST",
url: "DataTable.aspx/GetSummary",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
tblData = result.d;
}
});
}

my DataTable is empty, but I get the results from the web method.

What am i doing wrong?

This question has an accepted answers - jump to answer

Answers

  • wathertonwatherton Posts: 33Questions: 6Answers: 0

    http://debug.datatables.net/esihiy

    debugger, looks like it's trying to wright each piece of the data individually. Not the actual values.

  • john_ljohn_l Posts: 45Questions: 0Answers: 12
    Answer ✓

    The web service call is asynchronous - so it's likely that it will not have completed by the time your code tries to initialize the datatable. You should initialize the datatable in the ajax call's success callback (or in a function that you call from there).

  • wathertonwatherton Posts: 33Questions: 6Answers: 0

    Doooh, of course. Thanks John_i

  • allanallan Posts: 61,744Questions: 1Answers: 10,111 Site admin

    You could also use rows.add() in the load callback function to add the data to a DataTable which has already been initialised.

    Allan

This discussion has been closed.