Ajax in vanilla

Metodo classico

Invio di una richiesta ajax post che ritorna un dato di tipo testo

let http = new XMLHttpRequest();
http.onreadystatechange = function () {
    if (this.readyState == 4) {
        let res = this.response;
        document.getElementById('cerca-cliente').innerHTML = res;
    }
};
http.open('POST', '/service/cercaCliente', true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.send('term=' + p1);

Metodo fetch

Invio di una richiesta ajax per recuperare dati in formato json

fetch('/service/getCliente/' + id)
.then(response => response.json())
.then(data => compila(data));

functiob compila(data){
    console.log(data);
}