【问题标题】:Stripe checkout error条纹结帐错误
【发布时间】:2018-05-27 22:03:21
【问题描述】:

我正在尝试对我的商店实施条带结帐,但我收到一条错误消息:

这是我的代码:

onToken = (token) => {
  fetch('/save-stripe-token', {
    method: 'POST',
    body: JSON.stringify(token),
  }).then(response => {
    response.json().then(data => {
      alert(`We are in business, ${data.email}`);
    });
  });
}

【问题讨论】:

    标签: reactjs stripe-payments stripe.js


    【解决方案1】:

    似乎将对象解析为 json 时出错。知道你用什么调用 onToken 会很有帮助。

    确保在发出请求时将Content-TypeAccept 标头设置为application/json

    fetch('...', {
      // ...
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      // ...
    })
    

    确保始终添加一个 catch 块来处理错误。另外我建议您返回 response.json() 而不是在同一个 then 块中立即处理(这是一种反模式,无助于缓解回调地狱)。

    fetch(...)
      .then(response => {
        return response.json();
      })
      .then(data => {
        alert(`We are in business, ${data.email}`);
      })
      .catch(error => {
        // Handle the error here in some way
        console.log(error);
      });
    

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 1970-01-01
      • 2016-05-09
      • 2015-03-17
      • 2015-11-05
      • 2020-12-13
      • 2023-04-03
      • 2016-08-19
      • 2016-06-01
      相关资源
      最近更新 更多