0

I was trying to post data to Google Sheets like this https://gist.github.com/willpatera/ee41ae374d3c9839c2d6

But after sending a request I always get an error

"Access to fetch at 'https://script.google.com/macros/s/AKfycbzTxvHO3paUHMib0SY3Lo1uIEBumEGB6z1ILgMlw3fgK6LaKOs/exec' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled."

What I do wrong?

I was trying to set Mode: "no-cors" it didn't help.

formSubmit = async (e) => {
        e.preventDefault();
        try {
            let form = this.state.data;
            console.log("11111" + serialize(form, {hash: true, empty: true}));
            const request = await fetch('https://script.google.com/macros/s/AKfycbzTxvHO3paUHMib0SY3Lo1uIEBumEGB6z1ILgMlw3fgK6LaKOs/exec', {
                method: "POST",
                data: JSON.stringify({data: this.state.data})
            });

            const responseData = await request.json();

            console.log(
                "GET ResponsePost",
                "Response Body -> " + JSON.stringify(responseData));

            return JSON.stringify(responseData);
        } catch (e) {
            console.log(e);
        }
    }

I want to understand why this https://gist.github.com/willpatera/ee41ae374d3c9839c2d6 works (I checked) and my code don't work.

player0
  • 124,011
  • 12
  • 67
  • 124
Stas Motorny
  • 141
  • 1
  • 8
  • 1
    What's your script returning? If it returns invalid content, you get this CORS error: https://stackoverflow.com/a/55496419/9337071 Have you checked your script's Stackdriver logs to see if your function is throwing errors? – tehhowch Apr 07 '19 at 19:50
  • 1
    For example, when the script of Google Apps Script is directly run by giving a sample value on the script editor, does the script work fine? – Tanaike Apr 07 '19 at 22:16

0 Answers0