Quantcast
Channel: DataTables 1.10 — DataTables forums
Viewing all articles
Browse latest Browse all 2364

How to load table from $.ajax() request

$
0
0

I'm working on a "Hello World" scenario with a simple Web API project.

While I can successfully make an ajax call to my URL and show the results in the console, what I do not yet understand is how to load the $.ajax result into a Data Table.

I have looked at Ajax and it seems the key to loading my table is by use of this function, but I am unclear how to use this function in the case of my specific scenario. The examples I saw looked like they were pulling static files rather than get requests from a URL. (Or maybe I misunderstood what I was looking at.)

     "ajax":function(data, callback, settings) {
          /*...code... */
      }

To provide some more context, here is the full code block: my $,ajax() function that correctly displays the data on the console and the placeholder for the DataTable.

My question is this: how do I make my ajax call within the DataTable() initialization block?

$(() => {

    // this is the DataTable initializer where I would like to apply the ajax call
    $(".display").DataTable({
        "ajax":function(data, callback, settings) {

                // I don't know what to place here

        }
    });

    // this ajax function sends credentials required for authorization
    // and receives the data from the API.


    $.ajax({
        url: 'http://testdomain/api/testtables',
        method: "GET",
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            // loop data to console to verify it is
            // arriving to browser

            $.each(data, function(a, b) {
                console.log(b);
            });
        }
    });

});

Viewing all articles
Browse latest Browse all 2364

Trending Articles