ajax.url().load() : How to use POST request method instead of GET?

ajax.url().load() : How to use POST request method instead of GET?

ggarciaaggarciaa Posts: 3Questions: 2Answers: 0
edited November 2019 in Free community support

FROM THE DOCUMENTATION:

ajax.url().load()
Trigger a load of an Ajax data source when a URL has been set using the ajax.url() method.
Note ajax.url() must be used as a setter to set the URL for the load() method to be available in the returned object.

WHAT I WANT

I want to load always the same URL (namely 'index.php') BUT I need to pass new arguments via POST request parameters. Thus, index.php will process the POST request to get the new values I pass.

MY PROBLEM

It seems the only way to pass new parameters is via the URL, i.e. using GET method. I want to use POST. I do not see how.

When I first load the page, this is the JS initialization code:

.DataTable({
  ajax: {
    method: 'POST',
    data: {tipo: "download", folder: folder}
  }
});

The default URL is the current page, which is index.php.
This code sets POST as the ajax method.
It also passes two parameters in the 'data' object. These two parameters, I need to change in successive calls. I do not see how.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @ggarciaa ,

    It's because

        data: {tipo: "download", folder: folder}
    

    will treat those values as constants, so as you say, they won't change.

    If you make ajax.data a function, then you can set it dynamically, something like this:

        data: function(d) {
          d.tipo = "download";
          d.folder =  folder;
       }
    
    

    Cheers,

    Colin

  • ggarciaaggarciaa Posts: 3Questions: 2Answers: 0

    Colin:
    Thanks for your answer. I have managed to define the data option as a function, and ti have it take the object that I dinamically built But all of this happens just the first time, when I initialize de table.
    How do I call this funtcion the second time, and the third?

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949
    edited November 2019

    In this test case ajax.url().load() uses the function defined by ajax.data.
    http://live.datatables.net/xulakola/1/edit

    You can see this by looking at the Developer Tools > Network > Header tab to see the parameters sent to the server. If this isn't working for you please post a link to your page or a test case (update mine) replicating the issue.

    Kevin

This discussion has been closed.