There is a problem with ajax json response for c# framework upper then 3.5
There is a problem with ajax json response for c# framework upper then 3.5
Dearl All,
I have been using your framework and I believe I've found a problem and also corrected it.
For the C# frameworks upper than 3.5 all the json that is returned from a webmethod is encapsulated with a d for security reasons (http://stackoverflow.com/questions/6588589/why-do-asp-net-json-web-services-return-the-result-in-d?noredirect=1&lq=1#answer-6588603), which means that the json retrieved does not follow the base standart you have defines as you can see below:
{d: "{"draw":5,"recordsTotal":464,"recordsFiltered":464, "data":…}"}
Saying that when we are performing the UpdateDraw - function _fnAjaxUpdateDraw(settings, json)
-, we should guarantee the following:
if (json.hasOwnProperty("d"))
json = JSON.parse(json.d);
Without doing that, even if in the method _fnAjaxDataSrc(settings, json);
we perform the parse and return the data from the above object we will never be able to correctly set the values related with recordsTotal and recordsFiltered, which means we will have our data actualized but a problem with the pagination itself.
So the final method should be:
function _fnAjaxUpdateDraw(settings, json) {
// v1.10 uses camelCase variables, while 1.9 uses Hungarian notation.
// Support both
if (json.hasOwnProperty("d"))
json = JSON.parse(json.d);
var compat = function (old, modern) {
return json[old] !== undefined ? json[old] : json[modern];
};
var data = _fnAjaxDataSrc(settings, json);
var draw = compat('sEcho', 'draw');
var recordsTotal = compat('iTotalRecords', 'recordsTotal');
var recordsFiltered = compat('iTotalDisplayRecords', 'recordsFiltered');
if (draw) {
// Protect against out of sequence returns
if (draw * 1 < settings.iDraw) {
return;
}
settings.iDraw = draw * 1;
}
_fnClearTable(settings);
settings._iRecordsTotal = parseInt(recordsTotal, 10);
settings._iRecordsDisplay = parseInt(recordsFiltered, 10);
for (var i = 0, ien = data.length ; i < ien ; i++) {
_fnAddData(settings, data[i]);
}
settings.aiDisplay = settings.aiDisplayMaster.slice();
settings.bAjaxDataGet = false;
_fnDraw(settings);
if (!settings._bInitComplete) {
_fnInitComplete(settings, json);
}
settings.bAjaxDataGet = true;
_fnProcessingDisplay(settings, false);
}
Do you agree with this solution? Do you think you can incorporate it in a future release?
Best Regards