簡體   English   中英

如何將數據從 Node js 傳遞到 React 或從服務器重定向頁面

[英]How to pass a data from Node js to React or redirect a page from the sever

最終目的是將支付頁面重定向到成功頁面,但這發生在服務器上,我從 Stripe api 收到了一個對象響應,我想有條件地將其發送到前端(React),以便我可以重定向頁面或執行直接來自后端 謝謝

條紋按鈕

     <div className={styles.buttonsection}>
    <StripeCheckout 
amount={total*100}
label="Credit Card"
name="Boston Maids"
email=""
token={ (token) =>{axios.post('/api/stripe', token)}} 

stripeKey={"*********************"}


/>

服務器.js

app.post('/api/stripe', async (req, res) => {

   const charge = await stripe.charges.create({
        amount: 5000,
        currency: 'usd',
        description: 'Cleaning Services',
        source: req.body.id


    }).catch(console.error).then(charge =>{if (charge) {
     console.log(charge);

    } ```

創建一個新的方法來調用后端 API。 在該 API 的成功回調上,將結果設置為 state,然后處理您想做的任何事情。

讓我們說,在上面的例子中,如果你想填充令牌,從狀態中的結果設置令牌並使用this.state.token (如果你的狀態鍵是來自后端的令牌的令牌),如下所述:

<StripeCheckout 
amount={total*100}
label="Credit Card"
name="Boston Maids"
email=""
token={this.state.token} 
stripeKey={"pk_test_yBMj0cqYIXUsXeJ0lnVBil1T"}
/>

我只是在外面創建了一個函數

```const handlerToken =  async (token, total2) => {


     const response = await axios.post("/api/stripe", {
        token,
        total2 ```



```  <StripeCheckout
                  amount={total * 100}
                  label="Credit Card"
                  name="Boston Maids"
                  email=""
                  token={handlerToken}
                  stripeKey={"*****************"}
                /> ```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM