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

99
Visualizações
Arrays in AJAX request JSON object data

I am sending a request to a Node/Express server using jQuery thats data is a JSON object containing an array:

var data = {
    "name": "James Jamesy",
    "children": [
        {
            "name": "Tiny James",
            "age": "4"
        },
        {
            "name": "Little James",
            "age": "6"
        },
        {
            "name": "Graham",
            "age": "8"
        }
    ]
}

var request = $.ajax({
    method: 'PUT',
    url: apiPath + 'updateuser',
    data: data,
    dataType: 'json'
});

The request itself is working fine, however the server is reporting the data as:

{ 
    name: 'James Jamesy',
    'children[0][name]': 'Little James',
    'children[0][age]': '4',
    'children[1][name]': 'Medium James',
    'children[1][age]': '6',
    'children[2][name]': 'Graham',
    'children[2][age]': '8'
}

Now I've figured out that I can get my desired result by instead stringifying the children array:

var data = {
    "name": "James Jamesy",
    "children": JSON.stringify([ ... ])
}

And then JSON.parse()ing it on the server.

However I am hoping someone can explain why the array is converted as it is in the request, and whether I should be handling this a different way? As in this instance converting the single array is fine, but going forward I might have semi-complex objects I'm looking to send to the server.

Thanks in advance!

EDIT: Additionally and strangely(?), if I send the JSON result back as the passed JSON, it works perfectly:

res.json(JSON.parse(req.body.categories));

The browser logs out the object and I can manipulate it perfectly fine.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You weren't passing a JSON string through ajax which is why you couldn't handle the data on the back end.

var data = {
    "name": "James Jamesy",
    "children": [
        {
            "name": "Tiny James",
            "age": "4"
        },
        {
            "name": "Little James",
            "age": "6"
        },
        {
            "name": "Graham",
            "age": "8"
        }
    ]
}

var request = $.ajax({
    method: 'PUT',
    url: apiPath + 'updateuser',
    data: JSON.stringify(data),
    contentType: 'application/json', // for request
    dataType: 'json' // for response
});
about 4 years ago · Santiago Trujillo Relatório
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