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

233
Views
How to replicate Postman POST API request using ajax

I can currently use Postman to retrieve an "access_token" but I'm trying to replicate this in ajax (for the purpose of playing around with a few things in jsfiddle)

So in Postman, I have:

  • A POST request URL: https://login.microsoftonline.com/***/oauth2/token
  • No active headers
  • Body, consisting of 4 key-values (see below)

enter image description here

If I run that I get a response containing my access_token:

enter image description here

I'm trying to replicate this in ajax and after some help on here I was able to create the following script:

$.ajax({
  type: 'POST',
  url: 'https://login.microsoftonline.com/***/oauth2/token',
  data: JSON.stringify({
    grant_type: 'client_credentials',
    client_id: '***',
    client_secret: '***',
    resource: 'https://analysis.windows.net/powerbi/api'
  }),
  success: data => {
    console.log(data.access_token)
  },
  error: (xhr, textStatus, error) => {
    console.log('rr', error)
  }
});

This returns an error each time in the console:

enter image description here

I feel like I'm close but can not figure it out

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your POST is x-www-form-urlencoded but your ajax data is a json string.

When data is passed as a string it should already be encoded using the correct encoding for contentType, which by default is application/x-www-form-urlencoded.

When data is an object, jQuery generates the data string from the object's key/value pairs unless the processData option is set to false. For example, { a: "bc", d: "e,f" } is converted to the string "a=bc&d=e%2Cf". If the value is an array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below). For example, { a: [1,2] } becomes the string "a%5B%5D=1&a%5B%5D=2" with the default traditional: false setting.

https://api.jquery.com/jquery.ajax/

So try removing the JSON.stringify and just passing the POJO. e.g.

$.ajax({
  type: 'POST',
  url: 'https://login.microsoftonline.com/***/oauth2/token',
  data: {
    grant_type: 'client_credentials',
    client_id: '***',
    client_secret: '***',
    resource: 'https://analysis.windows.net/powerbi/api'
  },
  success: data => {
    console.log(data.access_token)
  },
  error: (xhr, textStatus, error) => {
    console.log('rr', error)
  }
});
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!