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

129
Visualizações
How can we convert ajax GET request into fetch API?
function myFunction(username, mess_id) {
        mess = document.getElementById(mess_id);
        mess.innerText = "Sending...";
        $.ajax({
                type: 'GET',
                url: '/user/resend',
                data: {usr:username},
                success: function(data){
                        mess.innerText = data;
                }
        })
}

In fetch API I have written as:

message = document.getElementById('mess_id');
message.innerText = 'Sending...';
fetch(url)
.then(data => data.text())
.then(mess => message.innerText = mess)

But, what about the data attribute of the ajax function.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're trying to send a GET request, which should hold data only in the query string, and not in the request payload.

Therefore, you can use this to send the user:

function myFunction(username, mess_id) {
        mess = document.getElementById(mess_id);
        mess.innerText = "Sending...";
        $.ajax({
                type: 'GET',
                url: '/user/resend?usr=' + username,
                success: function(data){
                        mess.innerText = data;
                }
        })
}

However, if you still want to send data: {usr:username} in the request body, you can send a POST request like this using fetch:

async function postData(url = '', data = {}) {
  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, etc.
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return response.text();  JavaScript objects
}

postData('/user/resend', {usr:username})
  .then(data => {
    console.log(data);
  });
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