![](/img/trans.png)
[英]Redirect REST API response to UI/Browser NodeJs/Express/Request
[英]How to redirect and render the url gotten from an API response object on the browser using express and axios
我正在使用 paystack API 和 Axios 构建支付网关以发出 http 请求。
我使用了他们的 API 文档并检查了他们的 postman 集合以了解如何编写代码。
该代码适用于 postman,因为每当我发送一个 post 请求以使用所需参数初始化事务时,我都会收到包含我需要的数据的响应 object(在本例中为 url)
问题是,我无法让我的应用程序重定向到浏览器上响应数据 object 中包含的 url,我在浏览器 window 上不断收到此错误
无法获取/paystack/undefined
已经查找了问题,尝试了一些可能的解决方案,从重构代码到使用 async await 但它不起作用(除了可能会稍微提高响应速度)
这是代码
Controller
const { default: axios } = require("axios");
exports.postForm = async (req, res) => {
await axios({
method: 'post',
url: 'https://api.paystack.co/transaction/initialize',
headers: {
'Authorization': secretKey,
'Content-Type': 'application/json',
'cache-control': 'no-cache'
},
data: {
"email": req.body.email,
"amount": req.body.amount
}
})
.then((response) => {
res.status(302).redirect(response.data.authorizaton_url) // it is supposed to go to the url inside the data object
console.log(response.data)
})
.catch((error) => {
console.log(error);
});
};
试图将 response.data 字符串化,但它没有改变任何东西
航线
const express = require('express');
const router = express.Router();
const pay = require('../controllers/paymentController');
router.get('/', pay.getForm);
router.post('/paystack/pay', pay.postForm);
Ps - 除非我将用户重定向回我的应用程序以确认交易,否则 GET 路由无济于事。 使用了另一个名为 Request 的 http 库,它可以工作,但已被弃用。
您可以在 paystack 上设置回调 url 以在成功付款时重定向用户,您可以在后台查看。 网络挂钩 url
如果这没有帮助,您可以考虑使用 res.status(301).redirect( your auth url
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.