【发布时间】:2019-04-11 16:23:50
【问题描述】:
使用 ExpressJs 和 ClaudiaJs 我已将 Web 服务器发布到 AWS Lambda。该服务器的工作是处理 Stripe 付款。我正在尝试让我的 React SPA 从客户端提交 Stripe 结帐,但是当我尝试提交时收到 CORS 错误。
如何避免此 CORS 错误?我想我需要在同一个 TLD 上同时发布客户端和 AWS 服务器(不知道如何使其工作),或者我需要在服务器上禁用 CORS(但这似乎不安全)。
条带化服务器调用:
this.handler = StripeCheckout.configure({
key: test_key,
locale: "auto",
mode: "no-cors",
name: "Company 1234",
description: "donation",
token: token => {
// Send the donation to your server
console.log("server pinged");
fetch(`${backendUrl}/charge`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
stripeToken: token,
chargeAmount: this.state.donationAmount
})
})
.then(res => res.json())
.then(json => {
console.log("response is " + json);
this.setState({ donationResponse: "Thank you for donating!" });
})
.catch(error => {
this.setState({
donationResponse:
"There was an issue processing your request. Please try again later"
});
});
}
});
表单提交
formSubmit = async event => {
console.log("form submitted");
event.preventDefault();
const amount = this.state.donationAmount * 100; // Needs to be an integer in cents
this.handler.open({
amount: Math.round(amount)
});
};
【问题讨论】:
标签: amazon-web-services cors aws-lambda single-page-application