0

我正在尝试对我的商店实施条带结帐,但我收到一条错误消息:在此处输入图像描述

这是我的代码:

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}`);
    });
  });
}
4

1 回答 1

0

看起来将对象解析为 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);
  });
于 2017-12-13T22:19:40.053 回答