Possible documentation oversight - ajax as a function

Possible documentation oversight - ajax as a function

RichardD2RichardD2 Posts: 26Questions: 2Answers: 1

I've been using the function-based version of the ajax option since v1, using an async function to invoke a fetch request.

This has worked fine. But when I tried upgrading to v3, I managed to mess up my "DataTables defaults" script, so that it didn't set serverSide: true. And that causes quite a few errors!

The particular error I noticed was calling ajax.reload(), which would throw an error: xhr.abort is not a function:

Demo

With the serverSide: true setting restored, the error goes away:

Demo

NB: I initially thought this might be a bug in v3, but when I tried to create a demo with the same version that currently works for me (2.3.0), I got the same error.

This only seems to happen if serverSide is false, and the ajax function returns a Promise (as all async functions do). It seems that the Promise gets stored in the jqXhr field, and the reload method tries to call jqXhr.abort(), which fails.

Would it be worth adding a note to the documentation to warn about this scenario?

Replies

  • allanallan Posts: 65,884Questions: 1Answers: 10,960 Site admin

    Hi,

    Thanks for flagging this up! There was some expectation that the return from the function would be an XMLHttpRequest object, rather than a Promise, however, that was a bit short sighted of me. Indeed, I've wondered before about adding support for a Promise return that will resolve to the JSON object, and perhaps I'll do that at some point in future.

    For the moment, I've put a guard around the abort call and your example updated with the nightly works as expected now. That will be in DataTables 3.0.1 which I'll tag and release tomorrow.

    Allan

Sign In or Register to comment.