首页 > 解决方案 > 当我将它从快递传递到反应时,重定向 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();
}

标签: javascriptreactjsexpressaxios

解决方案


推荐阅读