page keeps giving Reload every time and Fetch function is executed

Asked

Viewed 12 times

-1

const form = document.getElementById('form')
        form.addEventListener('submit', function (e) {
            e.preventDefault();

            const firsName = document.getElementById('firstname')
            const lastName = document.getElementById('lastName')
            const email = document.getElementById('email')
            const age = document.getElementById('age')
            const API_URL = 'http://localhost:3000/users';
            fetch(API_URL, {
                method: 'POST',
                body: JSON.stringify({
                    firsName,
                    lastName,
                    email,
                    age
                }),
                headers: {
                    "Content-Type": "application/json; charset=UTF-8"
                }
            }).then((response) =>response.json())
                .then((data) => {
                    console.log(data)
                })

        })
  • 1

    The function fetch does not re-load the page, has some other reason for this, most likely the event of submit is not being prevented, but only with what you posted can not help. The id of your form is form even?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.