javascript - 当我将它从快递传递到反应时,重定向 302 不起作用
问题描述
我有一个 Express 应用程序,在我的 API 中,我向客户端发送了 302 状态。客户端使用 React 和 AXIOS。它抛出这个错误:
Access to XMLHttpRequest at 'https://sandbox.zarinpal.com/pg/StartPay/000000000000000000000000000000012807' (redirected from 'http://localhost:5000/api/payment/x/handle/accepted')
from origin 'null' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource
这是我的 API
router.post('/x/handle/accepted', (req, res) => {
const uuidv4 = require('uuid/v4');
zarinpalLib.request(req.body.acamount, req.body.acemail, req.body.acphone, req.body.acdescription, uuidv4(), function (data) {
if (data.status) {
console.log('here ', data.url)
res.writeHeader(302, { 'Location': data.url });
res.end();
} else {
console.log('here2 ', data.code)
// res.render('error', {zpTitle: appConfig.appTitle, zpError: data.code});
res.status(500).send(data.code)
}
});
});
这是反应部分
if (inputValue === 'RLS') {
var data = {
acfirstname: 'xx',
aclastname: 'xx',
acemail: 'xx',
acphone: phoneNumber,
acamount: inputAmount,
acdescription: 'xx'
};
const res = await;
axios({ method: 'POST', url: `${END_POINT_URL}/api/payment/x/handle/accepted`, data });
console.log(res)
}
我在我的 Express 应用程序中为 CORS 使用了这个中间件:
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Headers', 'Content-Type,x-phone-token,Location');
next();
}
解决方案
推荐阅读
- java - 如何去除操作栏下的阴影
- python-3.x - 使用while循环的帕斯卡数
- postgresql - 如何使用 shell 脚本在 postgresql 中执行过程调用?
- html - 如何在较小的屏幕上重新定位一些块?
- sql - 从存储过程获取记录时出错
- docker - 如何使用 Docker 为开发和生产设置单独的 .env
- c# - 如何以编程方式获取 TabControl 的子项
- web-scraping - 我什么时候必须设置标题以及如何获取它们?
- java - 如何使用多个复选框和提交按钮来检索 Firebase 实时数据库数据
- swift - 如何在嵌套的 Firebase 异步 observeSingleEvent() 方法 Swift 中使用 DispatchGroup?