字符串化不会产生预期的json

问题描述 投票:0回答:1

这是使用Stripe.js的任何人的问题。我正在尝试使用express和node构建支付系统。我一直坚持这个问题大约一天。我只想用{"token":"token_val","item":"item_val"}发布一个json对象。我非常接近完成它,但当我将数据发布到我的支付路线时,我的json对象搞砸了。我得到了{'{"token":"token_val","item":"item_val"}': ''}形式的json。

var stripeHandler = StripeCheckout.configure({
    key: stripePublicKey,
    locale: 'en',
    token: function(token){
        var cartItem = document.getElementById("Monthly").id;
        var data = [{stripeTokenId: token.id, items: cartItem}];
        fetch('/purchase', {
          method: "POST", // *GET, POST, PUT, DELETE, etc.
          mode: "cors", // no-cors, cors, *same-origin
          cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
          credentials: "same-origin", // include, *same-origin, omit
          headers: {
            // "Content-Type": "application/json",
            "Content-Type": "application/x-www-form-urlencoded",
          },
          redirect: "follow", // manual, *follow, error
          referrer: "no-referrer", // no-referrer, *client
          body: JSON.stringify(data) // body data type must match "Content-Type" header
        })
    }
  })

这篇文章是否有问题引起了这个问题?我似乎无法理解为什么我得到这个带有空值的json obj键。我尝试了两种不同的内容类型,但似乎没有什么真正有所作为。

json stripe-payments stringify
1个回答
0
投票

问题是我没有使用express.json()。我将app.use(express.json())添加到我的app.js文件中,这修复了一切。我希望这可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.