How to redirect user with POST data in ReactJS?

Viewed 3160

I need to redirect user to external url with POST data. How could I achieve this in ReactJS?

I need to redirect the user to external bank deposit link. I thought bank link will respond whit redirect url but it doesn't. My thinking is that I need to make a POST request to that bank link and redirect user to that page. Tell me if I'm wrong. Here is what I've tried so far.

This redirect happens in dispatch so rendering a form is not an option

fetch(body.success.message.redirect_url, {
  method: "POST",
  body: JSON.stringify(body.success.message.request_data)
})
.then(response => {
  console.log(response);
})
.catch(error => {
  throw error;
});
1 Answers

Create a form element and submit it. This is how jquery-ujs does it. Obviously you need to rewrite this basic code to react, but you get the idea.

  var href = body.success.message.redirect_url,
    form = $('<form method="post" action="' + href + '"></form>'),

  form.hide().appendTo('body');
  form.submit();
Related