Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGlaukio Llupo
9,552 PointsHow to prevent a page from reloading on a post request
Hi, I am making an web application using NodeJS, pug, client-side JS and css. When someone fills out a form, the page doesn't stop reloading, i tried the following:
const form = document.getElementById("mainForm");
form.addEventListener("submit", (e) => {
e.preventDefault();
});
That stopped the page from reloading, but my server was not getting any info back. Can someone please help? Thanks in advance (NOTE: I do not want to redirect to a new page)
1 Answer
Steven Parker
231,141 PointsYou're partway there. Now that you have caught the submit event and disabled the default handling, send the data to the server as an AJAX request but just don't do anything when it succeeds.
Glaukio Llupo
9,552 PointsGlaukio Llupo
9,552 PointsHow do i do it with a post request (the AJAX)? Can you give me step by step please? Thanks!
Steven Parker
231,141 PointsSteven Parker
231,141 PointsI'd be willing to help if you give it your best "good faith" try and have trouble making it work.
If you haven't done them already, you might want to take the AJAX Basics and Working with the Fetch API courses first.