Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

300
Visualizações
Why can't I pass an array through a function?

I'm setting up all my variables before I'll start to call other functions. For that I need gridData from the database. I use AJAX to pull that data and use GET variables to use for the database query.

function loadPage() {
    console.log("loading Page");

    //prepare Data
    let _GET = pullGET();
    let gridID = _GET.get("gridID");
    let gridData = pullGridData(gridID);

    console.log(gridData);
    //start rending the Page
}

When I try to console log the GridData, it gives me "undefined".

function pullGridData(gridID) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {

            let DATA = JSON.parse(this.responseText);
            console.log(DATA[0]);
            return DATA[0];
        }
    };
    xhttp.open("POST", "action/PullGridData.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var header = "gridID=" + gridID;
    //console.log(header);
    xhttp.send(header);
}

It is weird that when I console log the "DATA" that I'm trying to pass along, I get what I expect.

I am aware that jQuery might have some good solution, but I'm trying to implement this with just pure JS so I can learn on.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda