Ajax Call with New API give Sys Parameter Count err; Old API works fine
Ajax Call with New API give Sys Parameter Count err; Old API works fine
JGiunta
Posts: 3Questions: 1Answers: 0
All,
Having trouble with the 'ajax' namespace with DataTables 1.10.7. I can issue a native Ajax call fine, but when I use the DataTables API I get a Sys.ParameterCountException.
If I use the old API, it works fine.
Here is an example which works fine:
$.ajax({
type: 'POST'
, url: 'ExpBudgetEmploye.aspx/GetEmployeSummaryAlt'
, contentType: 'application/json;'
, data: String.format("{{'DataSource': '{0}' }}", _dbId);
, dataType: 'json'
, success: function (data) {
alert(data.d[0].EmpName);
}
});
And using the old API which works fine:
oTable = $('#tblEmployeSummary').DataTable({
'processing': true
, 'serverSide': true
, 'jQueryUI': true
, 'ordering': false
, 'paging': false
, 'scrollCollapse': true
, 'scrollY': '200px'
, 'scrollX': '100%'
, 'sAjaxSource': 'ExpBudgetEmploye.aspx/GetEmployeSummaryAlt'
, 'sAjaxDataProp': 'd'
, 'columns': [
{ "title": "EmpName", "data": "EmpName", "type": "string", "className": "EmpName" }
, { "title": "EmpNumber", "data": "EmpNumber", "type": "string", "className": "EmpNumber" }
, { "title": "JobCode", "data": "JobCode", "type": "string", "className": "JobCode" }
, { "title": "HireDate", "data": "HireDate", "type": "date", "className": "HireDate" }
]
, 'fnServerData': function (sSource, aoData, fnCallback) {
$.ajax({
'dataType': 'json'
, 'contentType': 'application/json;'
, 'type': 'POST'
, 'url': sSource
, 'data': String.format("{{DataSource: '{0}' }}", _dbId)
, 'success': fnCallback
});
}
});
But using the new 1.10 way, I get an error. Has anyone had similar trouble?
Here is the code I am using with the new API, which gives me the sys parameter count exception.
oTable = $('#tblEmployeSummary').DataTable({
'processing': true
, 'serverSide': true
, 'jQueryUI': true
, 'ordering': false
, 'paging': false
, 'scrollCollapse': true
, 'scrollY': '200px'
, 'scrollX': '100%'
, 'ajax': ({
type: 'POST'
, url: 'ExpBudgetEmploye.aspx/GetEmployeSummaryAlt'
, contentType: 'application/json;'
, data: String.format("{{'DataSource': '{0}' }}", _dbId)
, dataType: 'json'
, dataSrc: 'd'
})
, 'columns': [
{ "title": "EmpName", "data": "EmpName", "type": "string", "className": "EmpName" }
, { "title": "EmpNumber", "data": "EmpNumber", "type": "string", "className": "EmpNumber" }
, { "title": "JobCode", "data": "JobCode", "type": "string", "className": "JobCode" }
, { "title": "HireDate", "data": "HireDate", "type": "date", "className": "HireDate" }
]
});
This discussion has been closed.
Answers
Can you link to the page so I can debug it please? I presume the
String.format("{{'DataSource': '{0}' }}", _dbId)
stuff just resolves to a simple string, Or is it a JSON string or something else? It might be that you need to useajax.data
as a function to add data to the object that DataTables sends.Allan
Yes what I am passing into ajax.data is just resolves to "{ 'DataSource': '46' }". In this case the page method is expecting one argument. Just for simplicity the page method is:
I get back an EmployeSummary object filled in.
Stepping through jquery.dataTables.js I found that I had ajax.data up to a point and then it was gone. I think I tracked it down to around line 2275. Specifically when data is set to either newData or the $.extended data. That IF returns and empty object with no useful properties. So the $.extend() is not doing what it should be doing correctly. I haven't futzed around with jQuery in quite some time an I need to re-familiarize myself with what exactly $.extend() does.
I "fixed" it by just adding a line underneath that says data = newData, but I realize I probably messed something else up with this hack.
From jquery.dataTables.js around line 2275:
If I change my ajax.data property to a function it works. Still don't understand why the $.extend is not merging the two objects properly though.
Because
$.extend
expects an object, but it looks like you are giving it a string. If you addedJSON.parse( ... )
to get an object from your string it would probably work.Allan