ajax.reload callback function not being passed JSON data

ajax.reload callback function not being passed JSON data

rotorboyrotorboy Posts: 18Questions: 4Answers: 0

I have a table defined:

 myTable = $('#myTable').DataTable (....);

As per https://datatables.net/reference/api/ajax.reload(), I have the following line in a function declared, though outside the context scope of the table definition :

 myTable.ajax.reload(function(json){ console.log(json)});

Though the function IS called after reload, json is null.

thoughts?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    Your code works here:
    http://live.datatables.net/fafuhihu/1/edit

    Please update the test case to show the issue.

    Kevin

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0
    edited July 2020

    Hey Kevin;

    Thanks for getting back to me. Indeed it does. I even tried to come closer to what I actually have by way of code structure and it still works

    http://live.datatables.net/fafuhihu/3/

    The main difference is I have the $('#example').DataTable().reload in the callback of a $.post() call. Not sure that should make any difference, but when the .post "done" callback fires, it runs $('#example').DataTable().reload, but the reload callback function(json) has "unintialized" for the json value.

    Clearly I'm doing something "different" / "wrong"; I should be able to reach it via the "API" call I'm using, right? Any ideas?

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    Can you post a link to your page or a test case replicating the issue so we can take a look? If no maybe post the Ajax call you are having a problem with.

    Kevin

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Hey Kevin;

    Page is behind a login so not really possible, but I have provided essentially the code structure I'm using here: http://live.datatables.net/satuhoxu/1/edit. obviously 'test.php' would need to be substituted with a real php script on a server somewhere.

    Any insight you can provide would be helpful.

    Thanks

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,954

    Thanks for the test case. I updated it a bit to work with the live.datatables.net ajax backend.
    http://live.datatables.net/yaremudo/1/edit

    The difference is using ajax as a function. @allan or @colin will need to let us know why the ajax.reload callback has undefined.

    Kevin

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Thanks for doing that.

    Perhaps since "ajax" is now a user-defined function, I need to set a "special" variable in the function to ensure that reloaded data makes it to the reload callback?

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Any word from @allan or @colin on this?

  • allanallan Posts: 63,514Questions: 1Answers: 10,472 Site admin
    Answer ✓

    This is an interesting one. So the issue as it stands is that the callback for ajax.reload() gets its data from ajax.json(). Looking at the source for that we see that it gets the data from the settings object’s json property. That is set in the default Ajax handler.

    That is not used in your example, since ajax is being given as its own function. This why it isn’t working.

    To make it work you could use settings.json = data; inside your getJSON: http://live.datatables.net/yaremudo/2/edit .

    Many thanks to yourself and Kevin for prep-ing the test case there!

    Allan

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Allan;

    You are welcome... glad to be able to provide a very small value add to the DT ecosystem... And thanks for answering.

    I had figured it had something to do with using my own ajax handler function, and was hopeful the "fix" was something as simple as stated.

    I could argue both ways on whether philosophically this is a bug or not, but either way, it would be good to add your reply to the documentation of specifying a "custom ajax handler", in case someone else stumbles across this.

    Thanks again, and long live DataTables....

This discussion has been closed.