How do you load data from ajax when the page loads?

3

What I want to do is that when entering the page, a table with the information of the users is loaded, but using JS, I do not like to put PHP code in the HTML, the question is that I do not know how to apply it through ajax, Data will be sent, which would be: $.post("Datos.php", datos_a_enviar, exito) , in which data is the PHP page, data_a_send_ the variables, and I succeed the function that is executed when the post succeeds.

What I do not know is how to load the data with ajax once the page is loaded, I saw that using window.onload will execute a function to load the page, but what I want to know is how I get them through ajax to display them in the vista ?, try $.post("Tomardatos.php, exito) , in which to take data is where I execute the sql and I succeed the function that is executed to show it, it was obvious that it was not going to work, but I try to curose it, I do not know if they understand me what I want to do xD, I hope you can answer me:)

    
asked by Shum 28.07.2018 в 02:13
source

1 answer

4

an example with jquery:

$(document).ready(function() {
    // pagina cargada ejecutar ajax o fetch await, etc. ...

    por ej. con jquery...
    $.ajax({
        url: '...prg.php?algo=1',
        method: "POST",
        dataType: "json",
        data: {enviardatosaphp:1, otro:2}
    }).
    done(function(res) {
        // respuesta de php con json_encode(array)
        console.log(res);
    });

});

an example with javascript:

document.addEventListener('DOMContentLoaded', () => {
    // pagina cargada ejecutar ajax o fetch await, etc. ...

});
    
answered by 28.07.2018 в 03:10