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

221
Views
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 answers
Answer question

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 Report
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!