JavaScript How to load Datatables from JSON object directly on Ajax success result? load datatables from json,load datatables from ajax,datatables load

After long time searching here around I can't find a solution to this problem, loading Datatables via Ajax GET is well documented but how can I use directly a JSON response after an Ajax POST?

This is my PHP function:

function getRelated() {
    var elements = (document.getElementsByClassName('exashare'));
    var query = [];
    for(var i=0;typeof(elements[i])!='undefined';query.push(elements[i++].getAttribute('data-id')));

    query = query.join(',');

    $.ajax({
        type: "POST",
        url: baseUrl+"/requests/get_related.php",
        data: "query="+query+'&_token='+_token, 
        cache: false,

        success: function(html){
            $('#example').DataTable( {
                "processing": true,
                "serverSide": true,
                "ajax": {
                    'url':jQuery.parseJSON(html),
                    "dataSrc": ""
                },
                "columns" : [
                        { "data": "#" },
                        { "data": "id" },
                        { "data": "art" },
                        { "data": "name" },
                        { "data": "title" },
                        { "data": "tag" },
                        { "data": "likes" },
                        { "data": "views" },
                        { "data": "duration" },
                        { "data": "time" }
                   ]
            });
        }
    });
}

The JSON on success looks like this:

{
    "data": [{
        "#": "1",
        "id": "9",
        "art": "default\/def8.jpg",
        "name": "Simon The Cat",
        "title": "Riflessioni sulla vita",
        "tag": "riflessioni,vita,",
        "likes": "1",
        "views": "1024",
        "duration": "185",
        "time": "2015-11-30 19:36:31"
    }, {
        "#": "2",
        "id": "15",
        "art": "default\/def2.jpg",
        "name": "Simon The Cat",
        "title": "Riflessioni sulla morte",
        "tag": "riflessioni,morte,",
        "likes": "1",
        "views": "1003",
        "duration": "185",
        "time": "2015-11-14 12:06:21"
    },
    ...]
}

I also tried this:

//from this:
$('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
        'url':jQuery.parseJSON(html),
        "dataSrc": ""
    },
    ...

//to this:
$('#example').DataTable( {
    "ajax": jQuery.parseJSON(html),
    ...

But on console it always shows me the error "lenght of undefined".

I already tried to load data directly from a JSON file url containing the same response here above and it works well.

How can I load JSON data on Ajax POST success results to populate my table?

Answer:1

I have a video the being played. How can I call a function 5 seconds before the end of the video? I thought to put a timer when I start the video, the problem is that the user can control the video, ...

I have a video the being played. How can I call a function 5 seconds before the end of the video? I thought to put a timer when I start the video, the problem is that the user can control the video, ...

I want to check if a server is reachable with a JavaScript function. By reachable, I mean, if the server answers, I don't care which HTTP status code, it's reachable. Stackoverflow/Google helped me ...

I want to check if a server is reachable with a JavaScript function. By reachable, I mean, if the server answers, I don't care which HTTP status code, it's reachable. Stackoverflow/Google helped me ...

  1. online check deposit resource center

I've read that you should avoid nested promises in JavaScript, as they tend to be an antipattern, but I'm having trouble figuring out how to avoid them in my particular use case. Hopefully someone ...

I've read that you should avoid nested promises in JavaScript, as they tend to be an antipattern, but I'm having trouble figuring out how to avoid them in my particular use case. Hopefully someone ...

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...

I'm building a web app that has to work on very old browsers (IE 8, Firefox 20, etc). Can I rely on document.readyState being interactive and complete working correctly? Basically, I want to perform ...