Cómo puedo enviar un json

Cómo puedo enviar un json

KarlaPKarlaP Posts: 6Questions: 2Answers: 0
edited May 2022 in Free community support

Hola, me gustaría saber que tengo mal en mi código, ojalá alguien pueda ayudarme.
Si intento usar dataTable y traerme información por medio de POST para enviar unos datos, me marca error (Estado HTTP 400 – Bad Request) y no encuentro la forma correcta de enviar esos datos.
Esto es lo que tengo:

$('#tabla').DataTable( {
        processing: true,
        destroy: true,
        serverSide: true,
        ajax: {
            url: ip + '/decl/public/findPerson',
            type: "POST",
            contentType: "application/json",
            data: JSON.stringify({
                "nombres": nombre,
                "apellido1": apellido
            })
        },
        columns: [
            { "data": "nombre", title:"Nombre" },
            { "data": "apellido_paterno", title:"Apellido Paterno" },
            { "data": "apellido_materno", title:"Apellido Materno" }
        ]
    } );

Gracias

Answers

  • kthorngrenkthorngren Posts: 20,269Questions: 26Answers: 4,765

    HTTP 400 – Bad Request

    You will need to look at the server logs to determine why its responding with a 400 bad request.

    I think you will want to use ajax.data as a function. See the example code in the docs plus this running example.

    Kevin

  • KarlaPKarlaP Posts: 6Questions: 2Answers: 0
    edited May 2022

    Gracias por responderme pero no, lo que requiero es agregarle el json al momento de la petición
    $(document).ready(function() {
    $('#example').DataTable( {
    "processing": true,
    "serverSide": true,
    "ajax": {
    url: ip + '/dec-demo/public/findPublict',
    "type": "POST"
    },
    "columns": [
    { "data": "first_name" },
    { "data": "last_name" },
    { "data": "position" },
    { "data": "office" },
    { "data": "start_date" },
    { "data": "salary" }
    ]
    } );
    } );

    Aquí agregarle el json en data, pero al hacerlo así no funciona
    ajax: {
    url: ip + '/dec-demo/public/findPublict',
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({
    "nombres": nombre,
    "apellido1": apellido
    })

    },

  • KarlaPKarlaP Posts: 6Questions: 2Answers: 0

    Listo, lo resolví usando esto:

    ajax: {
                url: ip + '/decl-demo/public/findPublic',
                type: "POST",
                contentType: "application/json",
                data: function ( d ) {
                    return JSON.stringify({
                        "nombres": nombre,
                        "apellido1": apellido
                    });
                },
                dataSrc: 'result.data'
            },
    

    Lo comento por si a alguien más le sirve la información.
    Gracias

  • aavaglianoaavagliano Posts: 1Questions: 0Answers: 0
    edited May 2022

    Hola KarlaP,

    Estoy con un problema similar tratando de llevar la data al server, pero me lo envía vacio:

                ajax: {
                    url: "{% url 'pedidos:buscar_mat_primas' %}",
                    type: 'POST',
                    dataType: 'json',
                    contentType: "application/json",
                    data: function ( d ) {
                        return JSON.stringify({
                            'proteina' : document.getElementById('proteina').value,
                            'cuarto' : document.getElementById('cuarto').value,
                            });
                    },
                    dataSrc: ""
                },
    

    No se que mas hacer, mi POST no se está llevando los datos seleccionados en el html

    Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

  • colincolin Posts: 15,142Questions: 1Answers: 2,586

    Have you tried the second example on the page ajax.data, i.e. just add those values in, something like:

        data: function ( d ) {
                d.proteina = document.getElementById('proteina').value;
                d.cuarto = document.getElementById('cuarto').value;
        },
    

    Colin

Sign In or Register to comment.