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

237
Visualizações
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 Respostas
Responde à pergunta

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