Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to redirect after a POST method using Fetch?

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

     
  });

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading