I am receiving the following errors when submiting an HTML
form
to Google Sheets
via Google Apps Script
:
Origin https://example.com is not allowed by Access-Control-Allow-Origin. Fetch API cannot load https://script.google.com/macros/s/…/exec due to access control checks. Unhandled Promise Rejection: TypeError: Origin https://example.com is not allowed by Access-Control-Allow-Origin.
On the server side, I’ve already set the XFrameOptionsMode.ALLOWALL
in order to load the response in an iframe
:
return HtmlService.createHtmlOutputFromFile("Index").setXFrameOptionsMode(
HtmlService.XFrameOptionsMode.ALLOWALL
)
What am I missing?
Here is my form:
document.getElementsByTagName("form")[0].setAttribute("target", "Response")
document.addEventListener("submit", e => {
const form = e.target
const iframe = Object.assign(document.createElement("iframe"), {
name: "Response"
})
document.body.innerHTML = iframe.innerHTML
fetch(form.action, {
method: form.method,
body: new FormData(form)
})
e.preventDefault()
})
<form action=https://script.google.com/macros/s/AKfycbzz-KveHder1A3CX8GcqZI6GR2MQj66PDRWNKoatIET_LXNqQs/exec method=post>
<fieldset>
<legend>Select Foobar</legend>
<label><input type=checkbox name=Foobar value=Foo>Foo</label>
<label><input type=checkbox name=Foobar value=Bar>Bar</label>
<label><input type=checkbox name=Foobar value=Baz>Baz</label>
</fieldset>
<input type=submit value=Submit>
</form>
The form submits to this Google Sheet
:
https://docs.google.com/spreadsheets/d/10VHS6bozcdNFYcRskkoONMT8Rt-2CwJ_LJGQWdkTJq4/
The web app is deployed with Me
execution privileges and Anyone, even anonymous
access.