I cannot display data into a table column by using ajax.

I cannot display data into a table column by using ajax.

laniasepsutisnalaniasepsutisna Posts: 1Questions: 1Answers: 0
edited February 2019 in Free community support

This is my code snippet. And I have trouble when I will display the data in a column that I have created.

$(document).ready(function() {

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

fetch_data('no');

function fetch_data(is_date_search, from='', to='')
{
    $('#table').DataTable({
        "processing" : true,
        "serverSide" : true,
        "order" : [],
        "ajax" : {
            method: 'POST', // Type of response and matches what we said in the route
            url: '{{ url('dataajax/search-periode') }}', // This is the url we gave in the route
            data: {
                'is_date_search': is_date_search, 
                'from': from, 
                'to': to,
            },
            columns: [  
                { data: 0 },
                { data: 1 },
                { data: 2 },
                { data: 3 },
                { data: 4 },
                { data: 5 },    
                { data: 6 },    
                { data: 7 },
                { data: 8 },
                { data: 9 },
                { data: 10 },
                { data: 11 },
                { data: 12 },
                { data: 13 },               
            ],

             // a JSON object to send back
            success: function(response){ // What to do if we succeed
                console.log(response); 
            },
            error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
                console.log(JSON.stringify(jqXHR));
                console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
            }
        }
    });
}

$('#search').click(function(){
    var from = $('#from').val();
    var to = $('#to').val();

    if(from != '' && to !=''){
        $('#table').DataTable().destroy();
        fetch_data('yes', from, to);
    }else{
        alert("Both Date is Required");
    }
}); 

} );

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,948

    What happens?

    Do you get any alerts or console log message?

    What data is returned from the server?

    The ajax docs mention that the success function shouldn't be used as it is used by Datatables. You will want to remove that part of your config.

    Kevin

This discussion has been closed.