JSON Formatting Error with C# WebMethod
JSON Formatting Error with C# WebMethod
Hello, I am getting the following error:
DataTables warning (table id = 'table'); DataTables warning:
JSON data from server could not be parsed. This is caused by a JSON formatting error.
I believe this is because when using a WebMethod in C# the JSON is formatted differently from normal JSON (Everything is wrapped in a d: IE {"d": 42} ).
My code:
JavaScript
[code]
$(document).ready(function () {
$('#table').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "SystemLog.aspx/GetSystemLog"
});
});
[/code]
C#
[code]
[System.Web.Services.WebMethod]
public static string[][] GetSystemLog()
{
return SystemLog.Get2DArray();
}
[/code]
I can successfully store the array of arrays in a javascript variable using a callback function, so I know that the JSON can be parsed and converted correctly outside of the dataTables library. Example:
[code]
var aaData;
function ScriptCallback(result) {
aaData = result;
}
[/code]
The above will correctly store the array of arrays in aaData when calling PageMethods.GetSystemLog(ScriptCallbakc), so I'm not sure why dataTables can't handle this directly?
I am not quite sure how to troubleshoot this error. Can anyone give any insight on this? Thanks!
DataTables warning (table id = 'table'); DataTables warning:
JSON data from server could not be parsed. This is caused by a JSON formatting error.
I believe this is because when using a WebMethod in C# the JSON is formatted differently from normal JSON (Everything is wrapped in a d: IE {"d": 42} ).
My code:
JavaScript
[code]
$(document).ready(function () {
$('#table').dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "SystemLog.aspx/GetSystemLog"
});
});
[/code]
C#
[code]
[System.Web.Services.WebMethod]
public static string[][] GetSystemLog()
{
return SystemLog.Get2DArray();
}
[/code]
I can successfully store the array of arrays in a javascript variable using a callback function, so I know that the JSON can be parsed and converted correctly outside of the dataTables library. Example:
[code]
var aaData;
function ScriptCallback(result) {
aaData = result;
}
[/code]
The above will correctly store the array of arrays in aaData when calling PageMethods.GetSystemLog(ScriptCallbakc), so I'm not sure why dataTables can't handle this directly?
I am not quite sure how to troubleshoot this error. Can anyone give any insight on this? Thanks!
This discussion has been closed.
Replies
Allan