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 make express app not change page on POST request

I have the following HTML form:

<form method="POST" action="/api/submit_agent">
    <label for="agent_type">Choose an agent model:</label>
    <select name="agent_type">
      <option value="male_agent" selected>Male Agent</option>
      <option value="female_agent">Female Agent</option>
    </select>
    <input id="submit_agent"type="submit" value="Submit">
</form>

and I want to process the form’s fields on my nodejs server like so (i’m using express)

app.post("/api/submit_agent", (req, res) => {
  // do stuff
});

but when I submit my form I get redirected to /api/submit_agent. Is there a way to have this processing not reflect in the browser aka without any type of redirection?

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

>Solution :

It’s not an express thing, just apart of how forms operate in HTML.

You can look at this answer which is similar:
Prevent redirect after form is submitted

Just make your post request via js instead of a submit action of your form.

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