Ajax reload fails when using a function and server side processing is false

Ajax reload fails when using a function and server side processing is false

ielcoroielcoro Posts: 2Questions: 1Answers: 0

When using a function like this and serverSide is set to false:

ajax: (d, c, s) => this.loadData(d, c, s)

Using ajax.reload fails, when it should be calling your custom function like when serverSide is enabled or when first loading data.

Looking through the code, it fails on __reload method when trying to cancel the previous xhr. Somehow, while initial data was loaded through the passed ajax function, a xhr object exists with and undefined readyState property, this passes the conditional check and tries to abort the request.

// Cancel an existing request
var xhr = settings.jqXHR;
if (xhr && xhr.readyState !== 4) {
    xhr.abort();
}

// Trigger xhr
_fnBuildAjax(settings, [], function (json) {
    _fnClearTable(settings);

    var data = _fnAjaxDataSrc(settings, json);
    for (var i = 0, ien = data.length; i < ien; i++) {
        _fnAddData(settings, data[i]);
    }

    _fnReDraw(settings, holdPosition);
        _fnProcessingDisplay(settings, false);
    });
}

I think the expected behavior should be not trying to abor the xhr, so the ajax function is called as always. I workaround this by externaly setting xhr to null before calling reload, but looks a lot like a hack,as I´m messing with internal settings objects.

Answers

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    What is your loadData function doing? I'm currently using ajax as a function quite extensively and have not ran into any issues using ajax.reload. I have serverSide: false

  • ielcoroielcoro Posts: 2Questions: 1Answers: 0

    My loadData function justs calls our own data loading functions that process parameters, perform authentication, etc, we do not use the jquery ajax object passed to it, we just get start and length parameters:

    loadData(data, callback, settings) {
       getDataCallback(data.start, data.length).then(d => {
           callback({
                  draw: data.draw,
                   recordsTotal: d.recordsTotal,
                   recordsFiltered: d.recordsFiltered,
                   data: d
              });
           });
    }
    
  • allanallan Posts: 63,470Questions: 1Answers: 10,467 Site admin
    edited September 2017

    If you are using server-side processing, you don't need draw, recordsTotal and recordsFiltered in the response.

    But that's by the by, the real issue I think is coming from here:

            // Is a function - let the caller define what needs to be done
            oSettings.jqXHR = ajax.call( instance, data, callback, oSettings );
    

    The internal xhr object in DataTables is being defined by whatever you return from the function that is being called (it looks like loadData()). From the above that is undefined, but that wouldn't trigger the error you are seeing. Is that the complete function?

    Can you link to a test page showing the issue please?

    Allan

This discussion has been closed.