首页 > 解决方案 > 重定向到外部 url 并使用 express 和 react 将数据作为正文参数发送

问题描述

我在前端有一个表单,它将数据发布到后端的API。然后,我向它添加额外的参数,即私钥并创建一个JSON 对象。在此之后,我必须在传递 JSON 对象进行付款时重定向到外部 URL(Payumoney)。

我无法将数据添加为查询参数,因为它包含敏感信息。

App.js(表单)

<form action="/api/payumoney" method="POST">
    <label>
      FirstName:
      <input type="text" name="firstname" onChange={handleChange} value={formValue.firstname} />
    </label>
    <label>
      TxnID:
      <input type="text" name="txnid" onChange={handleChange} value={formValue.txnid} />
    </label>
    <label>
      Amount:
      <input type="text" name="amount" onChange={handleChange} value={formValue.amount} />
    </label>
    <label>
      ProductInfo:
      <input type="text" name="productinfo" onChange={handleChange} value={formValue.productinfo} />
    </label>
    <label>
      Email:
      <input type="email" name="email" onChange={handleChange} value={formValue.email} />
    </label>    

    <input type="submit" value="Submit" />
  </form>

index.js

app.post("/api/payumoney", urlencodedParser, (req,res) => {

    const pd = req.body;  //JSON Object that we've to parse to the external URL
    const url = "https://sandboxsecure.payu.in/_payment";

    //Here goes something that can redirect to the URL and pass the JSON object
 }

index.js(使用请求)——它完成重定向并将数据传递给它的工作,但支付功能不起作用(附图片)

app.post("/api/payumoney", urlencodedParser, (req,res) => {

    const pd = req.body;  //JSON Object that we've to parse to the external URL
    const url = "https://sandboxsecure.payu.in/_payment";
    request.post({
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      uri: 'https://sandboxsecure.payu.in/_payment', //Testing URL
      form: pd,
    }, function (error, httpRes, body) {
      if (error){
        console.log("Error",error);
        res.status(400).json(
            {
                status: false, 
                message: error
            }
        );
     }
      if (httpRes.statusCode === 200) {
          res.send(body);
      } else if (httpRes.statusCode >= 300 && httpRes.statusCode <= 400) {
          res.redirect(httpRes.headers.location.toString());
          console.log("error 300 and 400");
    }
 })
}

使用请求解析数据并重定向到 Payumoney URL 后:

payuMoney支付网站错误

我正在使用代理来为客户端和服务器端点提供相同的来源。

谢谢!

标签: node.jsreactjsexpressmernpayumoney

解决方案


推荐阅读