Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!