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

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

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