Update Ajax request with new parameters

I get table data from a database through an AJAX request. And I need to change the data parameter in the AJAX request and update the table.

I update the table with the command

$('#table1').DataTable().ajax.reload();

I have the following code

$('#table1').DataTable({

    /* SERVER SIDE PROCESSING */
                "serverSide": true,
                "ajax":
                    {
                        "url": "Home/Search",
                        "type": "POST",

                        "data": {
                            'searchType': GetSearchType(),
                            'searchText': GetSearchText()
                            //'searchType': $.mynamespace.searchType
                            //'searchText': $.mynamespace.searchText
                            //'searchType': localStorage.getItem("searchType"),
                            //'searchText': localStorage.getItem("searchText"),
                        }
                    }
            });

But after the AJAX reboot, the original request to the server is sent, and the new parameter values ​​are ignored. I tried to pass the data to the request through a function, a global variable and browser storage, but none of the approaches worked. On the Internet, I find a solution with

aoData.push() 

function, but I don’t know how to use it.

My version of jQuery DataTables is 1.10.7.

I also tried to destroy and recreate the table with this code:

$('#table1').DataTable({
        "ajax":
            {
                "url": "Home/Search",
                "type": "POST",

                "data": {
                    'searchType': GetSearchType(),
                    'searchText': GetSearchText()
                }
            },
        "destroy" : true
    }).ajax.reload();

but I get an error:

DataTables: id = table1 - Ajax (http://www.datatables.net/manual/tech-notes/7)

'draw' 'System.Int32'

+10
5

ajax.data, .

, , , , .

$('#table1').DataTable({
   "serverSide": true,
   "ajax": {
      "url": "Home/Search",
      "type": "POST",
      "data": function(d){
         d.searchType = GetSearchType();
         d.searchText = GetSearchText();
      }
   }
});

$('#table1').DataTable().ajax.reload(), $('#table1').DataTable().ajax.reload(null, false), reset . . ajax.reload().

+23

, , , , .

$('#table1').DataTable({
            "iDisplayStart": 0,
            "iDisplayLength": 50,
            "bPaginate": true,
            "bSort": false,
            "serverSide": true,
             /* and all others settings others than default */
        "ajax":
            {
                "url": "Home/Search",
                "type": "POST",

                "data": {
                    'searchType': GetSearchType(),
                    'searchText': GetSearchText()
                }
            },
        "destroy" : true  /* <---- this setting reinitialize the table */
    }).

- , .

+2

:

var onSearchClick = function() {search();

    var search = function () {


        var startDate =  $('#datetimepicker1').find("input").val();
        var endDate = $('#datetimepicker2').find("input").val();

        $.ajax({
            type: "GET",
            url: "/api/getByDate?startDate=" + startDate + "&endDate="+endDate,
            datatype: "json",
            traditional: true
        })
        .done(function (data) {

          var table = $('#data-table-1').DataTable({
              data: data.data,
              destroy: true,
              "columns": [
             { "data": "id" },
             { "data": "id2" },
             { "data": "id3" }
              ],
              "columnDefs": [
           {
               "targets": [1],
               "visible": false,
               "searchable": false
           },
            {
                "targets": [2],
                "visible": false,
                "searchable": false
            }],
              "sPaginationType": "full_numbers"
          });
      });
    };
+1

AJAX . /. , beforeSend. .

Try this,

$scope.dtOptions = DTOptionsBuilder.newOptions()
.withOption('ajax', {
    url: 'url',
    'beforeSend': function (request) {
        request.setRequestHeader("Authorization","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9");
        request.setRequestHeader("Subscription-Key","1d64412357444dc4abc5fe0c95ead172");
    } ,

    //},
    type: "POST",
    cache: false, // It will not use cache url

})
+1
source
$('#table1').DataTable().ajax.url("?some_param=1&another=2").load();

This is another solution. Add your parameters to the default parameterizable parameters.

How to dynamically set Ajax URL for dataTable?

0
source

Source: https://habr.com/ru/post/1607238/


All Articles