-1
Hello,
On my page, when loading a JSON it should be integrated into the file. This works. What I wanted is for him to do this import only once, if not he loads the same array as many times as I request. I tried to create a boolean variable so that when I first clicked JSON, but then no more. You can help me?
var botaoAdicionar = document.querySelector("#btn-buscar");
botaoAdicionar.addEventListener("click", function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api-pacientes.herokuapp.com/pacientes");
xhr.addEventListener("load", function() {
var erroAjax = document.querySelector("#erro-ajax");
if( xhr.status == 200) {
erroAjax.classList.add("invisivel");
var resposta = xhr.responseText;
var pacientes = JSON.parse(resposta);
pacientes.forEach(function(paciente) {
adicionaPacienteNaTabela(paciente);
});
}else {
erroAjax.classList.remove("invisivel");
}
});
xhr.send();
});
Hi Patrick I don’t see any ajax in your code... you’ll have forgotten something?
– Sergio
Hi Sergio... I had copied the wrong file. Fixed.
– Patrick Thompson
Okay, so what about every click that should happen? that
.forEach
should still run if ajax is not needed becausepacientes
already exists? or all this code should run only once?– Sergio
What happens: every time I click the button, the array in the api is loaded on my page (a list of clients). What should happen: click once, load this array and if I click again, it should not (because it has already been loaded beforehand)
– Patrick Thompson