0

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:

enter image description here

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Nurat Jahan
  • 81
  • 1
  • 9
  • Can I ask you about the detailed setting of your deployed Web Apps? – Tanaike Feb 09 '23 at 11:51
  • I am running it from my local host. It just a simple web form. like form.html that's it – Nurat Jahan Feb 09 '23 at 11:53
  • Thank you for replying. I apologize for my poor English skill. I asked about the values of `Execute as` and `Who has access to the app` of your deployed Web Apps. By the way, in your situation, your Google Apps Script has no issues? – Tanaike Feb 09 '23 at 12:09
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – Gabriel Carballo Feb 09 '23 at 15:56

0 Answers0