假设我有一个表单,当我提交调用API时。该API返回一些数据,该数据最终显示在表单上。如果在这个API中,我想创建一个文件并将其返回给下载。我试过了,虽然总体上是有效的,但表单仍在等待回复。是否有办法欺骗表单以完成提交操作?
谢谢
发布于 2021-12-10 23:07:18
试着做这样的事:
const data = { username: 'john', email: 'test@test.com' };
fetch('https://example.com/api/test', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => {
// CREATE FILE HERE
response.json();
})
.then(data => {
// DOWNLOAD THE CREATED FILE HERE...
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});https://stackoverflow.com/questions/70311221
复制相似问题