【问题标题】:How to redirect from backend (nodejs) to frontend (react)?如何从后端(nodejs)重定向到前端(react)?
【发布时间】:2020-10-29 21:00:17
【问题描述】:

我想在域名为source.com 的网站上添加提交表单,当用户提交按钮时,我想将他重定向到域名为target.com 的网站。托管在 target.com 的应用程序的后端位于 backend.target.com,前端位于 frontend.target.com

source.com 我有:

<form action="https://backend.target.com" method="POST">
  <input type="text" id="textId" name="textName">
  <input type="submit" value="Submit">
</form>

我的问题是,我想在后端收到 post 请求后以某种方式在前端重定向用户。我怎样才能做到这一点?

【问题讨论】:

  • 如果我以错误的方式处理此问题,任何替代方法都会有所帮助。

标签: node.js reactjs redirect post callback


【解决方案1】:

我认为有这样的服务会更好。通过 REST 服务 (Look here) 提交表单,等待响应,然后根据该响应决定在哪里导航客户端。

你的客户可能是这样的

const requestOptions = {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title: 'React POST Request Example' })
};
fetch('http://someapi', requestOptions)
.then(response => //redirect to whereever)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    相关资源
    最近更新 更多