Enviar data por post en ajax

Enviar data por post en ajax

cesar2705cesar2705 Posts: 3Questions: 1Answers: 0
edited December 2019 in Free community support

Necesito enviar la opción seleccionada en un selected (html) por post al archivo llamado por el ajax, el cual que me carga el datatable. Pero no me esta funcionando.

Algo así:

manageCategoriesTable = $('#manageCategoriesTable').DataTable({
        'ajax' : {
            'url' : 'php_action/fetchCategories.php',
            'data' : { 'opselect' : opselect },
            'type' : 'post'
        },
        'order': []
    }); 

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    Can you give more details - saying it doesn't work doesn't give us much to help you with.

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • cesar2705cesar2705 Posts: 3Questions: 1Answers: 0

    Hola! Así estará bien: https://codepen.io/cesar2705/pen/YzPVzxe

  • kthorngrenkthorngren Posts: 21,166Questions: 26Answers: 4,921
    edited December 2019 Answer ✓

    Using { 'opselect' : opselect } is passing the HTML select into the ajax.data parameter causing the Uncaught TypeError: Illegal invocation error. You need to get the selected value or text, depending on which you want to pass in the parameter.

    Change your code to something like this to get the selected option's text:

    function cargarDataTable() {
        var selectedOption = $('#opselect').find(":selected").text();
        manageCategoriesTable = $('#manageCategoriesTable').DataTable({
            destroy: true,
            'ajax' : {
                'url' : 'php_action/fetchCategories.php',
                'data' : { 'opselect' : selectedOption },
                'type' : 'post'
            },
            'order': []
        }); 
    
    
    }
    

    Note the addition of the destroy option. If you don't have this then you will get the error in the technote on your second select.

    Kevin

  • cesar2705cesar2705 Posts: 3Questions: 1Answers: 0

    Hola! Muchas gracias kthorngren por la ayuda. Modifique mi función y todo funciona perfecto. Saludos!

This discussion has been closed.