Answer the question
In order to leave comments, you need to log in
How to process the response from a POST request?
There is a simple html form: <form method='post' action='/reg'></form>
On the server, I receive data like this via express:
app.post('/reg', (req, res) => {
res.json({data: 12345})
};
Answer the question
In order to leave comments, you need to log in
1. Simple request. The server must return the HTML. Use app.render, google
2. Asynchronous request. Here you need to catch the form submission on the client
var form = document.getElementById('reg-form');
form.addEventListener('submit', event => {
event.preventDefault(); // блокируем обычную отправку формы
var formData = new FormData(form);
fetch("/reg", {
method: "POST",
body: formData
})
.then(res => res.json())
.then(res => console.log(res.data)) // искомый ответ
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question