How to submit action error ajax.reload()

How to submit action error ajax.reload()

alehapalehap Posts: 2Questions: 1Answers: 0

Hi All;
please help
This code get data
**function fetPreview() {
$.ajax({
type: "POST",
text: "JSON",
url: "/CreateExample/fetandEditPreview",
//data: dataSend,
success: (function (data) {
var datas = JSON.parse(data),
dataTable = $('#preview').DataTable({

           dataSrc: "",
           data: datas,
           'searching': false,
            'lengthMenu': false,
            'lengthChange': false,
            'retrieve': false,
            'destroy': false,
            'sort': false,

            "language": {
                "paginate": {
                    "previous": "prev",
                    "next": "next"
                },
                "info": "",
            },
            columns: [
                { "data": "stt" },
                { "data": "name" },
                { "data": "startDate" },
                { "data": "endDate" },
                {
                    "data": "PK",
                    "render": function (PK) {
                        return '<a onclick="showPopup(' + PK +')" class="btn btn-default"><i class="fa fa-pencil"></i></a><a href="#" class="btn btn-warning"><i class="fa fa-trash"></i></a>'
                    }
                },
                //{
                //    "data": "PK",
                //    "render": function (PK) {
                //        return '<input type="checkbox" class="preview" name="previewPK" value="' + PK + '" id="' + PK + '" />'
                //    }
                //},
            ]
        });
    }),
})

}**

this code submit action
function updatePreview() {
//var dataTable;
$.ajax({
type: "POST",
text: "JSON",
url: form.action,
data: $(form).serialize(),
success: (function (data) {
if (data == "") {
alert("success");
$('#previewEdit').modal('hide');
displayAjaxLoading(false, 'Loading...');
$('#preview').DataTable().ajax.reload(null, false);
//dataTable.ajax.reload(null, false);
return;
}
}),
beforeSend: function (xhr) {
displayAjaxLoading(true, 'Loading...');
},
});
}

why error $('#preview').DataTable().ajax.reload(null, false);

DataTables warning: table id=preview - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1

please help me!

thanks all!

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119
    Answer ✓

    .For the reload to work, you need something like https://www.datatables.net/examples/ajax/objects.html where the ajax is part of the DataTable constructor.

    Your ajax is outside of the datatable object so it does not know about it and the reload thingy will not work.

  • alehapalehap Posts: 2Questions: 1Answers: 0

    bindrid

    Thanks, but create new table after submit action. :)

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    When you use ajax inside the Datatables as shown in the example, Datatables takes over the "success" portion of the ajax call. Because of that, Datatables can store the url needed to fetch the data as well as produce the table.

    When you use Ajax the way you did, datatables can only see the data you passed directly to it and does not know there was even an ajax call.

    If you want to keep things the way they are, you can but in the success, you would test to see if the table exists. If it does, you would simply use Datatables command to remove existing rows, add the new data and redraw the table.

This discussion has been closed.