Y
Y
Yevhenii K2019-09-06 20:17:10
JavaScript
Yevhenii K, 2019-09-06 20:17:10

How to send the user to another page with a POST request?

Tell me how to implement this task,
there is a form (textarea and a button), when submitting the form, you need to add an entry to the database, get its id and send the user to a different url using the POST method (with the id of the entry), then get the POST result from another url.
The example reminds me of authorization on sites using a third-party api (google, git).
My attempt at implementation through the request module, the problem here is that I don’t understand how to redirect the user to another site using the POST method with id.

router.post('/', function (req, res, next) {
  let text = req.body.text;
  let sessionID = req.session.id;
  db.query("INSERT INTO hc_posts (text,sessionID) VALUES (?,?)", [text, sessionID], function (err, result, fileds) {
    if (err) throw err;

    let formData = {
      id: "result.insertId",
    }

    // request.post({ url: 'https://another.site/post/', formData: formData }, function optionalCallback(err, httpResponse, body) {
    //   if (err) {
    //     return console.error('upload failed:', err);
    //   }
    //   console.log(httpResponse);
    //   console.log(body);
    //   res.redirect("/");
    // });

    res.redirect("/");
  });
});

Please explain how to organize this correctly and with what methods Node, Express.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2019-09-06
@Stalker_RED

You can redirect from the browser side using window.location (see href, assign or replace). And from the server side, by sending the Location
header . Whether the request had a POST or GET method, or some other, does not matter.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question