我正在開發一個帶有 Nodejs 后端的 React,并且我已經實作了“條紋”來處理付款。當我需要獲取應該將我重定向到 Stripe 支付表單的 URL 時,就會出現問題。我應該從 json 回應中獲取它,但無論我發送什么,它都是空的。我什至嘗試過發送非常簡單的資料,但它仍然不起作用。我之前在這個專案中使用過沒有問題,所以我不知道我在這里做錯了什么。任何人都可以提供任何幫助嗎?謝謝!
這是路由器檔案,它為支付創建會話,并且還應該發送所需的 URL。我測驗了,url是正確的,只是通過res.json發送就可以了
router.post("/payment", async(req, res) => {
const course = await Courses.findByPk(req.body.items[0].id);
const storeItems = new Map([
[course.id, { priceInCents: course.price, name: course.title }],
])
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
mode: 'payment',
line_items: req.body.items.map(item => {
const storeItem = storeItems.get(item.id)
return {
price_data: {
currency: "usd",
product_data: {
name: storeItem.name,
},
unit_amount: storeItem.priceInCents,
},
quantity: item.quantity,
}
}),
success_url: 'http://localhost:3000/profile-page',
cancel_url: `http://localhost:3000/course-details/${course.id}`
})
res.json({ url: session.url });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
這就是我應該取回 URL 的地方,但我沒有。相反,當我 console.log 它時,我得到“未定義”。
if (response.data.error) {
alert(response.data.error);
} else {
axios.post("http://localhost:3001/users_courses/payment", {
items: [
{ id: data.id, quantity: 1 },
],
}, {
headers: {
accessToken: localStorage.getItem("accessToken"),
},
}).then(res => {
if(res.ok) return res.json();
return res.json().then(json => Promise.reject(json));
}).then (( { url }) => {
window.location.href = url;
console.log(url " this is the url");
}).catch(e => {
console.error(e.error);
})
}
uj5u.com熱心網友回復:
我認為這與您處理axios帖子的方式有關,我認為像我在下面建議的那樣做一個小改動應該對您有用。
axios
.post(
"http://localhost:3001/users_courses/payment",
{
items: [{ id: response.data.id, quantity: 1 }],
},
{
headers: {
accessToken: localStorage.getItem("accessToken"),
},
}
)
.then(({ data: { url } }) => {
window.location.replace(url);
console.log(url " this is the url");
})
.catch((e) => {
console.error(e.error);
});
請注意,這axios與fetch您必須處理將回應主體轉換為 json 物件的 API 不同。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/497240.html
上一篇:如何映射嵌套的D3物件并創建陣列
下一篇:序列化后接收雙引號
