Get data Client Side using Ajax call
Get data Client Side using Ajax call
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
http://debug.datatables.net/esihiy
debugger, looks like it's trying to wright each piece of the data individually. Not the actual values.
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).
Doooh, of course. Thanks John_i
You could also use
rows.add()
in the load callback function to add the data to a DataTable which has already been initialised.Allan