在表单提交到Google Sheets时,使用以下代码来解决CORS错误:
const scriptURL = 'https://script.google.com/macros/s/你的脚本ID/exec';
const form = document.forms['your-form'];
const submitBtn = document.querySelector('.submit-btn');
form.addEventListener('submit', e => {
e.preventDefault();
submitBtn.disabled = true;
fetch(scriptURL, { method: 'POST', body: new FormData(form)})
.then(response => {
console.log(response);
submitBtn.disabled = false;
form.reset();
})
.catch(error => console.error('Error!', error.message))
})
请注意,scriptURL
应该是您的Google Apps Script Web应用程序的URL。使用此代码后,您应该能够成功地将表单提交到Google Sheets。
上一篇:表单提交到电子邮件的操作失败,请求方法变为GET,状态码为304或500。
下一篇:表单提交导致POST请求,触发登录页面中的UsernamePasswordAuthenticationFilter。