您可以使用JavaScript中的fetch函数来解决这个问题。以下是一个示例代码:
// 提交数据到第一个端点
fetch('/endpoint1', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (response.ok) {
// 提交数据到第二个端点
return fetch('/endpoint2', {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
});
} else {
throw new Error('Error submitting data to endpoint1');
}
})
.then(response => {
if (response.ok) {
// 重定向到感谢页面
window.location.href = '/thank-you-url';
} else {
throw new Error('Error submitting data to endpoint2');
}
})
.catch(error => {
console.error(error);
});
以上代码首先使用fetch函数将数据提交到第一个端点/endpoint1。如果成功,它会继续将数据提交到第二个端点/endpoint2。如果两个端点都返回成功的响应,那么页面将被重定向到感谢页面/thank-you-url。如果任何一步出现错误,它将在控制台打印错误消息。
请注意,这只是一个示例代码,您需要根据自己的实际需求进行修改和适应。