How do i allow a CORS requests in my google script?












1















I want to post my contact form to my google script that will send an e-mail to me. I use the following code:



var TO_ADDRESS = "example@gmail.com"; // where to send form data

function doPost(e) {

var callback = e.parameter.callback;

try {
Logger.log(e); // the Google Script version of console.log
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(callback+
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(callback+JSON.stringify({"result":"error",
"error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}


When i try to post to the google script url, i get the following error:




Access to XMLHttpRequest at
'https://script.google.com/macros/s/~~myscriptid~~/exec' from origin
'http://localhost:4200' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.




I have no clue how to add the CORS-filter to my google script.



I know the script is working i have tested it with this plugin:



https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi










share|improve this question























  • where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

    – skyboyer
    Nov 22 '18 at 15:23











  • The script runs in the google cloud, my application runs on localhost. It is a school assignment.

    – Joost Kaal
    Nov 22 '18 at 15:25











  • according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

    – skyboyer
    Nov 22 '18 at 15:31











  • The script runs on google cloud, and gets called by my angular application.

    – Joost Kaal
    Nov 22 '18 at 15:35






  • 1





    sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

    – skyboyer
    Nov 22 '18 at 16:07
















1















I want to post my contact form to my google script that will send an e-mail to me. I use the following code:



var TO_ADDRESS = "example@gmail.com"; // where to send form data

function doPost(e) {

var callback = e.parameter.callback;

try {
Logger.log(e); // the Google Script version of console.log
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(callback+
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(callback+JSON.stringify({"result":"error",
"error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}


When i try to post to the google script url, i get the following error:




Access to XMLHttpRequest at
'https://script.google.com/macros/s/~~myscriptid~~/exec' from origin
'http://localhost:4200' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.




I have no clue how to add the CORS-filter to my google script.



I know the script is working i have tested it with this plugin:



https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi










share|improve this question























  • where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

    – skyboyer
    Nov 22 '18 at 15:23











  • The script runs in the google cloud, my application runs on localhost. It is a school assignment.

    – Joost Kaal
    Nov 22 '18 at 15:25











  • according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

    – skyboyer
    Nov 22 '18 at 15:31











  • The script runs on google cloud, and gets called by my angular application.

    – Joost Kaal
    Nov 22 '18 at 15:35






  • 1





    sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

    – skyboyer
    Nov 22 '18 at 16:07














1












1








1








I want to post my contact form to my google script that will send an e-mail to me. I use the following code:



var TO_ADDRESS = "example@gmail.com"; // where to send form data

function doPost(e) {

var callback = e.parameter.callback;

try {
Logger.log(e); // the Google Script version of console.log
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(callback+
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(callback+JSON.stringify({"result":"error",
"error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}


When i try to post to the google script url, i get the following error:




Access to XMLHttpRequest at
'https://script.google.com/macros/s/~~myscriptid~~/exec' from origin
'http://localhost:4200' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.




I have no clue how to add the CORS-filter to my google script.



I know the script is working i have tested it with this plugin:



https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi










share|improve this question














I want to post my contact form to my google script that will send an e-mail to me. I use the following code:



var TO_ADDRESS = "example@gmail.com"; // where to send form data

function doPost(e) {

var callback = e.parameter.callback;

try {
Logger.log(e); // the Google Script version of console.log
MailApp.sendEmail(TO_ADDRESS, "Contact Form Submitted",
JSON.stringify(e.parameters));
// return json success results
return ContentService
.createTextOutput(callback+
JSON.stringify({"result":"success",
"data": JSON.stringify(e.parameters) }))
.setMimeType(ContentService.MimeType.JSON);
} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(callback+JSON.stringify({"result":"error",
"error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}


When i try to post to the google script url, i get the following error:




Access to XMLHttpRequest at
'https://script.google.com/macros/s/~~myscriptid~~/exec' from origin
'http://localhost:4200' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.




I have no clue how to add the CORS-filter to my google script.



I know the script is working i have tested it with this plugin:



https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi







google-apps-script cors






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 15:17









Joost KaalJoost Kaal

678




678













  • where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

    – skyboyer
    Nov 22 '18 at 15:23











  • The script runs in the google cloud, my application runs on localhost. It is a school assignment.

    – Joost Kaal
    Nov 22 '18 at 15:25











  • according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

    – skyboyer
    Nov 22 '18 at 15:31











  • The script runs on google cloud, and gets called by my angular application.

    – Joost Kaal
    Nov 22 '18 at 15:35






  • 1





    sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

    – skyboyer
    Nov 22 '18 at 16:07



















  • where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

    – skyboyer
    Nov 22 '18 at 15:23











  • The script runs in the google cloud, my application runs on localhost. It is a school assignment.

    – Joost Kaal
    Nov 22 '18 at 15:25











  • according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

    – skyboyer
    Nov 22 '18 at 15:31











  • The script runs on google cloud, and gets called by my angular application.

    – Joost Kaal
    Nov 22 '18 at 15:35






  • 1





    sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

    – skyboyer
    Nov 22 '18 at 16:07

















where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

– skyboyer
Nov 22 '18 at 15:23





where is your script expecting to be run? I believe currently you run it from localhost for development purposes. but how do you plan use it in the future?

– skyboyer
Nov 22 '18 at 15:23













The script runs in the google cloud, my application runs on localhost. It is a school assignment.

– Joost Kaal
Nov 22 '18 at 15:25





The script runs in the google cloud, my application runs on localhost. It is a school assignment.

– Joost Kaal
Nov 22 '18 at 15:25













according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

– skyboyer
Nov 22 '18 at 15:31





according to error message your script running at localhost is trying to access google cloud. But you say script should be run on google cloud. Am I missing something?

– skyboyer
Nov 22 '18 at 15:31













The script runs on google cloud, and gets called by my angular application.

– Joost Kaal
Nov 22 '18 at 15:35





The script runs on google cloud, and gets called by my angular application.

– Joost Kaal
Nov 22 '18 at 15:35




1




1





sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

– skyboyer
Nov 22 '18 at 16:07





sou CORS affects your angular application. where will it be run finally? there are different solutions possible depending on if it will run also at cloud,

– skyboyer
Nov 22 '18 at 16:07












1 Answer
1






active

oldest

votes


















0














As far as I understood you have application to be run on custom domain. And it should access script on google cloud.



The bad news: there are no way to skip CORS check on your application side(until request is simple that I believe is not your case).



You should specify Access-Control-Allow-Origin on Google Cloud side:




Cloud Storage allows you to set CORS configuration at the bucket level only. You can set the CORS configuration for a bucket using the gsutil command-line tool, the XML API, or the JSON API. For more information about setting CORS configuration on a bucket, see Configuring Cross-Origin Resource Sharing (CORS). For more information about CORS configuration elements, see Set Bucket CORS.



You can use either of the following XML API request URLs to obtain a response from Cloud Storage that contains the CORS headers:



storage.googleapis.com/[BUCKET_NAME]



[BUCKET_NAME].storage.googleapis.com




If this does not help for any reason you will need to get your own server working as a proxy:



your client application <-> your backend that returns Access-Control-Allow-Origin <-> google cloud






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433938%2fhow-do-i-allow-a-cors-requests-in-my-google-script%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As far as I understood you have application to be run on custom domain. And it should access script on google cloud.



    The bad news: there are no way to skip CORS check on your application side(until request is simple that I believe is not your case).



    You should specify Access-Control-Allow-Origin on Google Cloud side:




    Cloud Storage allows you to set CORS configuration at the bucket level only. You can set the CORS configuration for a bucket using the gsutil command-line tool, the XML API, or the JSON API. For more information about setting CORS configuration on a bucket, see Configuring Cross-Origin Resource Sharing (CORS). For more information about CORS configuration elements, see Set Bucket CORS.



    You can use either of the following XML API request URLs to obtain a response from Cloud Storage that contains the CORS headers:



    storage.googleapis.com/[BUCKET_NAME]



    [BUCKET_NAME].storage.googleapis.com




    If this does not help for any reason you will need to get your own server working as a proxy:



    your client application <-> your backend that returns Access-Control-Allow-Origin <-> google cloud






    share|improve this answer




























      0














      As far as I understood you have application to be run on custom domain. And it should access script on google cloud.



      The bad news: there are no way to skip CORS check on your application side(until request is simple that I believe is not your case).



      You should specify Access-Control-Allow-Origin on Google Cloud side:




      Cloud Storage allows you to set CORS configuration at the bucket level only. You can set the CORS configuration for a bucket using the gsutil command-line tool, the XML API, or the JSON API. For more information about setting CORS configuration on a bucket, see Configuring Cross-Origin Resource Sharing (CORS). For more information about CORS configuration elements, see Set Bucket CORS.



      You can use either of the following XML API request URLs to obtain a response from Cloud Storage that contains the CORS headers:



      storage.googleapis.com/[BUCKET_NAME]



      [BUCKET_NAME].storage.googleapis.com




      If this does not help for any reason you will need to get your own server working as a proxy:



      your client application <-> your backend that returns Access-Control-Allow-Origin <-> google cloud






      share|improve this answer


























        0












        0








        0







        As far as I understood you have application to be run on custom domain. And it should access script on google cloud.



        The bad news: there are no way to skip CORS check on your application side(until request is simple that I believe is not your case).



        You should specify Access-Control-Allow-Origin on Google Cloud side:




        Cloud Storage allows you to set CORS configuration at the bucket level only. You can set the CORS configuration for a bucket using the gsutil command-line tool, the XML API, or the JSON API. For more information about setting CORS configuration on a bucket, see Configuring Cross-Origin Resource Sharing (CORS). For more information about CORS configuration elements, see Set Bucket CORS.



        You can use either of the following XML API request URLs to obtain a response from Cloud Storage that contains the CORS headers:



        storage.googleapis.com/[BUCKET_NAME]



        [BUCKET_NAME].storage.googleapis.com




        If this does not help for any reason you will need to get your own server working as a proxy:



        your client application <-> your backend that returns Access-Control-Allow-Origin <-> google cloud






        share|improve this answer













        As far as I understood you have application to be run on custom domain. And it should access script on google cloud.



        The bad news: there are no way to skip CORS check on your application side(until request is simple that I believe is not your case).



        You should specify Access-Control-Allow-Origin on Google Cloud side:




        Cloud Storage allows you to set CORS configuration at the bucket level only. You can set the CORS configuration for a bucket using the gsutil command-line tool, the XML API, or the JSON API. For more information about setting CORS configuration on a bucket, see Configuring Cross-Origin Resource Sharing (CORS). For more information about CORS configuration elements, see Set Bucket CORS.



        You can use either of the following XML API request URLs to obtain a response from Cloud Storage that contains the CORS headers:



        storage.googleapis.com/[BUCKET_NAME]



        [BUCKET_NAME].storage.googleapis.com




        If this does not help for any reason you will need to get your own server working as a proxy:



        your client application <-> your backend that returns Access-Control-Allow-Origin <-> google cloud







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 9:09









        skyboyerskyboyer

        3,54111128




        3,54111128






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433938%2fhow-do-i-allow-a-cors-requests-in-my-google-script%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen