0

I posted form data to an API endpoint and I want to redirect the browser to the response text. I seem to not be able to figure out what I am missing. Below is what I have been able to do so far:

  const formEl = document.querySelector("form");
  formEl.addEventListener("submit", (event) => {
    event.preventDefault();
    const formData = new FormData(formEl);
   
    
    const data = new URLSearchParams(formData);
   
    fetch("https://example.com/users", {
      method: "POST",
      body: data
    })
  
       
     .then(async response => console.log(await response.text()))
     .then(window.location.href = (response.text()))

     
  });
Putkimies
  • 73
  • 7

1 Answers1

0

Hope this will help you.

fetch("https://example.com/users", {
    method: "POST",
    body: data
})
    .then(async response => response.text())
    .then(result => {
        window.location.href = result
    });
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Quentin Dec 21 '22 at 18:40