DataTable Ajax with Remote Data Source & JSON

DataTable Ajax with Remote Data Source & JSON

OzkarServicesOzkarServices Posts: 4Questions: 0Answers: 0
edited November 2012 in DataTables 1.9
Guys

Im trying to load data from a Remote Data Source and it keeps failing. Below is my Web Method developed in VB.NET.
[code]
_
Public Shared Function Users() As String

Dim TheFitFemContext As EntitiesContext = New EntitiesContext

Dim Results = (From U In EntitiesContext.Users

Select New With {U.UserID})

Dim serializer As New JavaScriptSerializer()

Dim json As String = serializer.Serialize(Results)


Return json

End Function
[/code]

Here's my jQuery Code

[code]
$('#example').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": ApplicationPath + "/Administrator/Users.aspx/Users",
"fnServerData": function (sSource, aoData, fnCallback) {
$.ajax({
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "POST",
"url": sSource,
"data": '{}',
"success": function (data)
{
fnCallback(data.d);
}
});
}
})
[/code]

When I run the code and I get the following as my Results

[code]
{"d":"[{\"UserID\":\"451\",\"Name\":\"John Doe\",\"Address\":\"123 Test Ave, DK, 11234\",\"Date_Created\":\"\\/Date(1327039200000)\\/\",\"Status\":\"Not Active\",\"IsLockedOut\":\"Not Locked Out\",\"Type\":\"Trial\",\"Email\":\"UserName@DomainName.com\",\"ResetPassword\":\"*\"}]"}
[/code]
and my table does not populate instead I get this error

[quote]
Error: Unable to get value of the property 'length': object is null or undefined
[/quote]

Replies

  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    Because its not a JSON object its a string. You need to decode the string. Use $.parseJSON .
  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    Oh - also if you are using server-side processing (which you are) you need to fully implement the protocol: http://datatables.net/usage/server-side
  • OzkarServicesOzkarServices Posts: 4Questions: 0Answers: 0
    I tried adding that call but its still failing with the same error message.

    [code]
    $('#example').dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": ApplicationPath + "/Administrator/Users.aspx/Users",
    "fnServerData": function (sSource, aoData, fnCallback) {
    $.ajax({
    "dataType": 'json',
    "contentType": "application/json; charset=utf-8",
    "type": "POST",
    "url": sSource,
    "data": '{}',
    "success": function (data) {
    var s = $.parseJSON(data.d);
    fnCallback(s);
    }
    });
    }
    })
    [/code]
  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    Then your JSON is not complete (most likely it isn't fully implementing server-side processing). Indeed I can't see how it possibly can if you aren't posting any data to the server.
  • OzkarServicesOzkarServices Posts: 4Questions: 0Answers: 0
    edited November 2012
    So the below result is not complete?

    [code]
    {"d":"[{\"UserID\":\"451\",\"Name\":\"John Doe\",\"Address\":\"123 Test Ave, DK, 11234\",\"Date_Created\":\"\\/Date(1327039200000)\\/\",\"Status\":\"Not Active\",\"IsLockedOut\":\"Not Locked Out\",\"Type\":\"Trial\",\"Email\":\"UserName@DomainName.com\",\"ResetPassword\":\"*\"}]"}
    [/code]
  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    No - did you look at the documentation for server-side processing? You need to have `iTotalRecords` , `iTotalDisplayRecords` and `sEcho` .

    I'd suggest you probably don't want server-side processing... http://datatables.net/usage/

    Allan
  • OzkarServicesOzkarServices Posts: 4Questions: 0Answers: 0
    Do you have any Example code for getting the data from ASP.NET Web Method with the aoData variables and passing it to the DataTable?
  • allanallan Posts: 54,578Questions: 1Answers: 8,533 Site admin
    A quick search of the forums lead me to this: http://www.datatables.net/forums/discussion/803/couple-questions-json-and-sajaxsource/p1#Item_8 . There are a number of other discussions about it as well.

    Allan
This discussion has been closed.