Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

121
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda