I am trying to post data from a form in node JS and then see the data in console. When i click the button I can see the data in console but the websites starts loading and it doesnt end. The loading circle doesnt stop moving.
JavaScript
x
5
1
<form method="post" action="/">
2
<input type="text" name="user[name]">
3
<input type="submit" value="Submit">
4
</form>
5
and this is in my server.js file
JavaScript
1
9
1
app.use(express.urlencoded({ extended: true }));
2
// Parse JSON bodies (as sent by API clients)
3
app.use(express.json());
4
5
// Access the parse results as request.body
6
app.post('/', function(request, response){
7
console.log(request.body.user.name);
8
});
9
Advertisement
Answer
Your backend has to send a response, e.g. response.sendStatus(200)
, otherwise the frontend will wait until it timeouts.
Example:
JavaScript
1
10
10
1
app.use(express.urlencoded({ extended: true }));
2
// Parse JSON bodies (as sent by API clients)
3
app.use(express.json());
4
5
// Access the parse results as request.body
6
app.post('/', function(request, response){
7
console.log(request.body.user.name);
8
response.sendStatus(200);
9
});
10