Problem retrieving data with two tables and POST requests with ajax().url(object).load()

Problem retrieving data with two tables and POST requests with ajax().url(object).load()

julioltjuliolt Posts: 3Questions: 1Answers: 0

Hi all,

I'm having an issue with post request. I have two different tables and I need to make a request when users click on the first table.
When an user clicks, a request is sent to the server so that I could draw the info.

So I create one table and add an event to one button on a <span>. When click, I try to reload the second table with:

oTable.ajax().url('http://www.example.com/object.json').load();

Everything seems ok through GET post. Reading in other related posts, I get a solution to make these requests through POST

https://datatables.net/forums/discussion/21940/how-to-pass-new-post-parameters-on-ajax-reload

So I need to pass to ajax.().url() an object with the configuration. I.e.:

object = {url: 'http:www.example.com', type: 'POST', data: {name: 'hey', surname: 'yo'}};

Now I try to get the data through:

oTable.ajax().url(object).load()

First try seems fine but when I try to give to it a second chance, it does not work.

First post: http://www.example.com/getData.php (with POST data)

Second post: http://www.example.com/[Object object] (WRONG PATH)

I found out that in datatable.js line 83104, we get the ajax.url() function definition. There, in the set section, depending on "settings.ajax", the function sets the param called "url" to "settings.ajax" or "settings.ajax.url".

Is that a mistake or a missunderstood from me? ajax.url() is always expecting a String so we could not load new data through POST?

Thank you very much in advance,

Julio


Related links: https://datatables.net/reference/api/ajax.url()

Datatable version: 1.10.11

ajax.url() function:

_api_register( 'ajax.url()', function ( url ) {
    var ctx = this.context;

    if ( url === undefined ) {
        // get
        if ( ctx.length === 0 ) {
            return undefined;
        }
        ctx = ctx[0];

        return ctx.ajax ?
            $.isPlainObject( ctx.ajax ) ?
                ctx.ajax.url :
                ctx.ajax :
            ctx.sAjaxSource;
    }

    // set

    return this.iterator( 'table', function ( settings ) {


         // HERE IS THE PROBLEM, IF "URL" is an object instead of an string containing the url


        if ( $.isPlainObject( settings.ajax ) ) {
            settings.ajax.url = url;
        }
        else {
            settings.ajax = url;
        }
        // No need to consider sAjaxSource here since DataTables gives priority
        // to `ajax` over `sAjaxSource`. So setting `ajax` here, renders any
        // value of `sAjaxSource` redundant.
    } );
} );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin

    The ajax.url() option only accepts a string, as noted in the documentation. Currently passing in a new configuration object for the Ajax construct is not something that is supported I'm afraid.

    Allan

  • julioltjuliolt Posts: 3Questions: 1Answers: 0

    Hi Allan,

    I readed the docs and I supposed what you just said but I found it a little bit strange. I mean, I should re-init or re-create (via datatable() method) the whole table each time I should make a post request instead of update the data of an existing datatable.

    It works when updating the core and setting the settings.ajax to the url argument. I found clearer that we should pass an argument to ajax.url() with the configuration of the table for ajax as an JS object instead of only the end point (string) where we get the data from.

    Thank you for your answer,

    Julio

  • allanallan Posts: 63,352Questions: 1Answers: 10,444 Site admin
    Answer ✓

    At this time there isn't a public API way to change the Ajax configuration after the table has been created. You would need to destroy and create a new table if that is something you need to do, or just make your own $.ajax call and use clear() and rows.add() to add the rows to the table.

    Allan

  • julioltjuliolt Posts: 3Questions: 1Answers: 0

    Thank you Allan, I'll give it a try by making the post and then add the data with clear() an rows.add().

This discussion has been closed.