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

327
Views
JS - Trying to send an array in an asychronous AJAX POST Request (to PHP)

I am trying to send a POST request, containing an array, but the array is appearing as empty in the payload. When logging the array to the console it shows as having a length of 2 (expected) and I can see the values.

I don't understand what I am doing wrong, all the articles I have read have been about an array not showing the expected values or returning undefined; I can clearly see the values are in the array when they are output to the console. Why is this not mirrored in the POST request? I am also receiving undefined if I try to console.log a specific value.

i.e. console.log(items[0]); //returns undefined

Code snippet:

function callPHP(items) {
    console.log(items);
    var xhr = new XMLHttpRequest();
    var data = items;
    var postUrl = *link*;
    xhr.open('POST', postUrl, true);
    xhr.send(data);
    xhr.onreadystatechange = function() { 
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
            console.log("Got response 200!");
        }
    };  
}

function getUserInfo(aCallback){
    var items = [];
    chrome.identity.getProfileUserInfo({'accountStatus': 'ANY'}, function(info) {
        email = info.email;
        items.push(email);
        items.push("test");
    });
        
    aCallback(items);   
}

document.onreadystatechange = function () {
    if (document.readyState === "interactive") {
        getUserInfo(callPHP);
    }       
};

I would really appreciate any help as I have been stuck on this for hours and I feel I may be missing something super obvious. If you need any more clarification on my code or its context, please let me know!

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!