fetch

fetch

0

 

sin parametros
<script>
    fetch('obtener-ruta')
        .then(res => res.json())
        .then(data => {


            data.forEach(element => {


                if (element.tipo_id == 1) {
                    element.tipo_id = 'Credito';
                } else {
                    element.tipo_id = 'Ahorro';
                }

                document.getElementById('contenido_tabla').innerHTML += `
                <div class="d-flex pt-3">
                    <div class="pb-3 mb-0 small lh-sm border-bottom w-100">
                        <div class="d-flex justify-content-between">
                            <div>
                                <strong class="text-gray-dark">${element.nombre}</strong>
                                <span class="d-block mt-1 text-muted">${element.tipo_id}</span>
                            </div>
                            <a class="btn btn-sm float-end" href="./flujo-ver/${element.id}"><i class="bi bi-box-arrow-in-right fs-5"></i></a>
                        </div>
                    </div>
                </div>
                `;
            });
        });

  
</script>


con parametros para eliminar

function Eliminar(numero) {
        // creo un fordata vacio
        const data = new FormData();

        // simulo los datos 
        data.append('id', numero);

        fetch('../ingreso-eliminar', {
                method: 'POST'// or 'PUT'
                body: data,

            })
            .then(res => res.json())
            .then(data => {

            });
    };


con los parametros de un formulario

// get my formulary propertys 
    let formulario = document.getElementById('formulario');

    // get the Event of form
    formulario.addEventListener('submit'function(e) {
        e.preventDefault();

        // get the fields values 
        var datos = new FormData(formulario);

        fetch('../AddIngreso', { // get the response
                method: 'POST',
                body: datos // i send my fields values
            })
            .then(res => res.json()) // i get my json response
            .then(data => {
                // this is my show dialog 
                Swal.fire({
                    title: 'Agregado correctamente!',
                    icon: 'success',
                    confirmButtonText: 'Ok',

                }).then(res => { // show dialog response
                    if (res.isConfirmed == true) {
                        location.reload(); // I reload the page
                    }
                })

            })
    });

No hay comentarios