Been following MDN fetch for a couple of days, I’ve written a simple example,
the user inputs username and password, and it gets sent to a server
const fetchOptions = data => {
return {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
}}
form.addEventListener("submit", (e) => {
e.preventDefault()
fetch(uri, fetchOptions({
"username":user.value,
"password":pwd.value
})).catch(e=>console.log(e))
})
On the server, I’ve this simple function (pseudocode)
app.post("/login", (req, res) => {
("if validated") => res.redirect(303, "/")
else ("error")
})
The file at endpoint “/” lands in the browser, (see picture below), but yet the page is not displayed. Ifwindow.location.href="/"
is used in fetch and this works (displays the response html page). This is not good practice because it generates a brand new request to “/”.
Could you help me understand why the page is not displayed?
Is it that we cant redirect a POST request?
The server is express.