我正在我的项目中实施PayPal,并且正在使用paypal-rest-sdk,并遵循此-> https://www.youtube.com/watch?v=7k03jobKGXM
[在执行res.redirect()
时,在前端,我从localhost:8000/api
重定向到原点null
)已被CORS策略阻止:对预检请求的响应未通过访问控制检查:无Access-Control-Allow-Origin
标头存在于请求的资源上。
我正在呼叫的api:
var create_payment_json = {
...
...
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
console.log('error... ',error.response);
throw error;
} else {
console.log('successs... ', payment);
for (var index = 0; index < payment.links.length; index++) {
//Redirect user to this endpoint for redirect url
if (payment.links[index].rel === 'approval_url') {
// console.log('approval url... ',payment.links[index].href);
res.redirect(payment.links[index].href);
}
}
}
})
main app.js:
const cors = require('cors');
var enableCORS = 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-Headers', 'Content-Type, token, Content-Length, X-Requested-With,Accept, *');
if ('OPTIONS' === req.method) {
res.sendStatus(200);
} else {
next();
}
};
app.all("/*", 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-Headers', 'Content-Type, token, Content-Length, X-Requested-With, Accept, *');
next();
});
app.use(enableCORS);
cors({credentials: true, origin: true})
app.use(cors());
我能够从前端调用其他API,并且在为PayPal执行res.redirect()
时遇到此错误。
请帮帮我。
我有同样的问题,我可以解决。贝宝(Paypal)将按钮代码作为带有动作的表单提供。我的问题是我已经改变了:
form action="https://www.paypal.com/cgi-bin/webscr"
到
action="javascript:myCheckFunction"
我执行了一些交叉检查,然后调用jquery $ .post函数将请求发布到PayPal。贝宝(Paypal)不喜欢这样:-)我改回了原始的“ action =”代码,并将交叉检查移到了其他地方(有效)希望这会有所帮助。