I am trying to save data in google sheet, so I am using google script https://script.google.com/a/macros/in-school.net/s/---------/exec
for that. In my flat HTML page I have used JavaScript for the POST method but after clicking the the submit button I get an error in my console log:
Here is my code:
<textarea id="subject" type="Subject" name="subject" placeholder="Please tell us How we can assist you" style="height:200px"></textarea>
<input type="submit" value="Submit" onclick="sample(this);return false;">
</form>
</div>
</body>
<script>
function sample(e) {
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
headers.append('Access-Control-Allow-Origin', 'http://localhost:3000');
headers.append('Access-Control-Allow-Credentials', 'true');
headers.append('GET', 'POST', 'OPTIONS');
const url = "https://script.google.com/a/macros/in-school.net/s/......../exec";
const obj = [...e.parentNode].reduce((o, f) => (o[f.name] = f.value, o), {});
console.log(obj);
const query = new URLSearchParams(obj);
fetch(url + "?" + query, { method: "POST",headers: headers })
.then((res) => res.json())
.then((res) => {
console.log(res);
alert(res.result);
});
}
</script>
I wanted to have row HTML website, create a simple form, after submitting the form my data will be saved in google sheet. I don't want to use any server side language.