Ajax POST custom Parameters

Ajax POST custom Parameters

MausinoMausino Posts: 61Questions: 19Answers: 0

https://datatables.net/manual/server-side#Sent-parameters

Hi, i implemented the server side search.. but i need also sent to post request for ajax some other parameters

My case:
I have Location input, distance dropdown, send button.
On server side i need get lat and long of central point, find to max distance.

But this i cant send by ajax post which make Datatables.
Must i hack some input field that values of location + distance will set to some parameter and i will create custom logic on server side for this or is same example how to send via post of Datable other params as defined in docs

https://datatables.net/manual/server-side#Sent-parameters

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    Answer ✓

    You can use ajax.data as a function to send other parameters to the server with the server side parameters.

    Kevin

  • MausinoMausino Posts: 61Questions: 19Answers: 0

    @kthorngren

    2 days i am trying read every possible documentation in Datatables + Forums and xyz example for symfony as backend (subreqests, services) and i din't see it in official docs

    Thank you for answer... i feel ashamed that i overlooked it

  • MausinoMausino Posts: 61Questions: 19Answers: 0
    edited August 2021

    If somebody is interested abou how it looks.. here is my example how post custom parameters.This is from Twig with Symfony 5 example

                        processing: true,
                        serverSide: true,
                        ajax: {
                            url: '{{ path('dt-process-data') }}',
                            type: 'POST',
                            async: true,
                            // dataSrc: ,
                            data: function ( d ) {
                                // i am sending to one php url.. by this parameter i know if go from DT or Editor the reqest to backend
                                // in editor ajax is the same name of parameter but value editor_DataTables
                                d.dataTables = 'viewer_DataTables';
                                // this values are send from form by send button
                                d.getSearchQuery = $('#getSearchQuery').val(); 
                                d.getSearchModule = $('#getSearchModule').val();
                                d.getSearchLocation = $('#getSearchLocation').val();
                                d.getSearchDistance = $('#getSearchDistance').val();
                                d.getSearchDistanceUnit = $('#getSearchDistanceUnit').val();
    
                                // own custom parameters
                                d.test1 = some_js_global_variable;
                                d.test2 =
                                    [
                                        'a', 'b', 'c', 'd',
                                    ];
                                // d.test3 = ['a'=>'b', 'b'=>'c']; // this doesn't works, use instead test4 structure
                                d.test4 = {
                                    a: 'Modul: {{ module }}',
                                    b: 'Search' {{ search }},
                                    c: 'Location: {{ location }}',
                                    d: 'Distance: {{ distance }}',
                                    e: 'Unit: {{ unit }}',
                                };
                            },
                        },
    
Sign In or Register to comment.