Bug in function based ajax data source

Bug in function based ajax data source

b_levittb_levitt Posts: 3Questions: 0Answers: 0

When setting the ajax object to a function, an error is thrown on line 3291 (ver 1.10.5). I traced the issue back to the following code:

function _fnAjaxDataSrc ( oSettings, json )
{
    var dataSrc = $.isPlainObject( oSettings.ajax ) && oSettings.ajax.dataSrc !== undefined ?
        oSettings.ajax.dataSrc :
        oSettings.sAjaxDataProp; // Compatibility with 1.9-.

The issue seems to be that ajax.dataSrc IS undefined so sAjaxDataProp is used and the errors cascade from there.

The same issue can be seen when doing the following:

  $('#DataTable').dataTable({
    "ajax":
      {
        url: 'myjsonurl',
        type: "POST"
      },

However, the code pointed me to setting dataSrc to empty string and that works successfully.

  $('#DataTable').dataTable({
    "ajax":
      {
        url: 'myjsonurl',
        type: "POST"
        dataSrc: ""
      },

Replies

  • b_levittb_levitt Posts: 3Questions: 0Answers: 0

    I did eventually find a reference that describes the different functionality when setting dataSrc to "" - arrays vs an object with a data property on it. Solution was to do this where data1 was my data array:

            callback({ data: data1 });
    
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Could you show me the code you were using when using ajax as a function please?

    Thanks,
    Allan

  • b_levittb_levitt Posts: 3Questions: 0Answers: 0

    This is the one that works:

        ajax: function (data, callback, settings) {
          $.post(
            '@this.Url.Action("OrdersByProduct", "DashboardData")',
            { From: $("#IptFrom").val(), To: $("#IptTo").val() },
            function (data1) {
              callback({ data: data1 });
            }
          );
    

    The key was wrapping the returned data1 array with a property "data";

    This is what threw an error (along with the scenario mentioned in the OP).

        ajax: function (data, callback, settings) {
          $.post(
            '@this.Url.Action("OrdersByProduct", "DashboardData")',
            { From: $("#IptFrom").val(), To: $("#IptTo").val() },
            function (data1) {
              callback(data1);
            }
          );
    
  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin

    Got it - thanks! Yes, there is actually no option to specify dataSrc when ajax is a function. That is definitely an oversight (which I must confess I'm not sure how to fix). I'll have a think about the best way of addressing this, but in the mean time, thanks for posting your workaround.

    along with the scenario mentioned in the OP

    What OP? Your message above is the first in this thread.

    Thanks,
    Allan

This discussion has been closed.