D
D
Dvorak2016-09-25 01:54:32
JavaScript
Dvorak, 2016-09-25 01:54:32

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})
};

Whether it is possible to accept somehow this request now on the client?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2016-09-25
@allishappy

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)) // искомый ответ
});

S
Sergey, 2016-09-25
@void01

jQuery.ajax()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question