这是我的代码:
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}`);
});
});
}
这是我的代码:
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}`);
});
});
}
看起来将对象解析为 json 时出错。知道你用什么调用 onToken 会很有帮助。
确保在发出请求时设置Content-Type
和Accept
标头: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);
});