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

222
Visualizações
How to pass an array of object into a payload in a JSON format while making a api request

I have an array of objects like this.

var arr = [
{name: 'myName', age: 25, city: 'myCity'},
{name: 'myName1', age: 25, city: 'myCity1'}
];

Now, I need to pass this arr array into a payload as a JSON object. I used JSON.stringify(arr) and then passed the data. But it is getting converted into a form-data and the payload format is like below;

[{name: 'myName', age: 25, city: 'myCity'},{name: 'myName1', age: 25, city: 'myCity1'}]:

If you have noticed a colon at the end of the payload, seems like it converted my array as a property name to the form data. Can you help me to pass the array as a JSON object.

Code that is used to call the API method is,

{
 type: 'POST',
 method: 'POST',
 dataType: 'json',
 url: 'url of the api call',
 beforeSend: function(header, settingsData){
    Object.keys(settingsData.options).forEach(headerKey){
       headerKey.setHeader(headerKey, settingsData.options[headerKey]);
    }
    return settingsData;
 },
 disableDefaultError: true
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

My comment was half correct.

  • Keep dataType: 'json'
  • add contentType: 'application/json' since the default is 'application/x-www-form-urlencoded; charset=UTF-8'
{
 type: 'POST',
 method: 'POST',
 dataType: 'json',
 contentType: 'application/json', // <- this is needed
 url: 'url of the api call',
 beforeSend: function(header, settingsData){
    Object.keys(settingsData.options).forEach(headerKey){
       headerKey.setHeader(headerKey, settingsData.options[headerKey]);
    }
    return settingsData;
 },
 disableDefaultError: true
}
about 4 years ago · Juan Pablo Isaza 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