如何从js前端的节点后端访问值
我想从我的节点后端访问js文件中的值。
这里是节点代码...节点:
(async () => {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [{
name: 'T-shirt',
description: 'Comfortable cotton t-shirt',
images: ['.png'],
amount: 500,
currency: 'eur',
quantity: ((req.body.tickets - 0) + (req.body.tickets_kid - 0)),
}],
success_url: 'http://localhost:3000/thank-you?session_id={session.id}',
cancel_url: 'https://localhost:3000/tickets',
});
res.send(session);
})();
...和js代码
const stripe = Stripe('secretkey');
const session = ?
console.log(session);
async function stripeCheckout() {
console.log('activated checkout')
const { error } = await stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: '{{session.id}}'
})
window.alert(error.message);
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `error.message`.
};
现在,我正在尝试将会话ID放入js文件。感谢您的帮助。
回答如下:您需要向后端发出一些请求才能获得响应。