2 years ago

#68215

test-img

Patrick Simard

Datatable returning 0 results

I need to load a datatable with a JSON dataset. I don't want to do server side processing. but I do want to retrieve the full set of data from a URL. I got to the point where there's no error on load but for some reason, the table stays empty with 0 results.

I am building the JSON on the server side like this:

public static function datatableOutput($array){

    foreach($array as $key=>$line){
        $output[] = [
            "href"=>$line['href'],
            "code"=>$line['code'],
            "time"=>$line['time'],
            "img_count"=>$line['img_count'],
            "int_count"=>$line['int_count'],
            "ext_count"=>$line['ext_count']
        ];
    }

    return array('data'=> $output);
}

Later in the code it returns a json string like this:

return response()->json($this::datatableOutput($links));

And in the view my script looks like this:

$.ajax({
    url: '/crawl',
    type: "post",
    data: {
        url: $("#url").val(),
        pages: $("#pages").val(),
        _token:"{{ csrf_token() }}"
    },
    dataType : 'json',
    success: function(data){

        // Done state
        $('#iamarobotnotaslave').hide();
        $('#justasecdude').hide();
        $('#crawl').hide();
        $('#report_section').show();

        $('#report').DataTable( {
            data: data,
            columns: [
                { title: "href" },
                { title: "code" },
                { title: "time" },
                { title: "img_count" },
                { title: "int_count" },
                { title: "ext_count" }
            ]
        } );
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        // Error state
        $('#iamarobotnotaslave').hide();
        $('#justasecdude').hide();
        $('#goaheaddude').show();
        $('#error').show();
    }
});

The JSON data set returned looks like this:

{
"data":[
    {
        "href":"\/",
        "code":200,
        "time":0.2746608257293701,
        "img_count":6,
        "int_count":204,
        "ext_count":6
    },
    {
        "href":"\/feature\/automated-marketing-reports",
        "code":200,
        "time":0.1753251552581787,
        "img_count":7,
        "int_count":72,
        "ext_count":6
    },
    {
        "href":"\/feature\/marketing-dashboard-2",
        "code":200,
        "time":0.15781521797180176,
        "img_count":8,
        "int_count":73,
        "ext_count":6
    }
]
}

Everything seems to be valid but for some reason I still get 0 results ... I am probably missing something obvious thought. I tried the JSON with and with out the "data" array.

javascript

php

jquery

laravel

datatable

0 Answers

Your Answer

Accepted video resources