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

330
Visualizações
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
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