如何在我的谷歌脚本中允许 CORS 请求? [英] How do i allow a CORS requests in my google script?

查看:17
本文介绍了如何在我的谷歌脚本中允许 CORS 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的联系表格发布到我的 google 脚本中,该脚本将向我发送电子邮件.我使用以下代码:

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);
  }
}

当我尝试发布到 google 脚本 url 时,我收到以下错误:

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

访问 XMLHttpRequest 在'https://script.google.com/macros/s/~~myscriptid~~/exec' 来自原点'http://localhost:4200' 已被 CORS 策略阻止:响应预检请求未通过访问控制检查:否请求中存在Access-Control-Allow-Origin"标头资源.

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.

我不知道如何将 CORS 过滤器添加到我的 google 脚本中.

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/详细信息/允许控制允许原点/nlfbmbojpeacfghkpbjhddihlkkiljbi

推荐答案

据我了解,您有要在自定义域上运行的应用程序.它应该访问谷歌云上的脚本.

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

坏消息:在您的应用程序端无法跳过 CORS 检查(直到 请求很简单我相信不是你的情况).

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).

您应该在 Access-Control-Allow-Originrel="nofollow noreferrer">谷歌云端:

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

Cloud Storage 仅允许您在存储分区级别设置 CORS 配置.您可以使用 gsutil 命令行工具、XML API 或 JSON API 为存储桶设置 CORS 配置.有关在存储桶上设置 CORS 配置的更多信息,请参阅配置跨域资源共享 (CORS).有关 CORS 配置元素的更多信息,请参阅设置存储桶 CORS.

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.

您可以使用以下任一 XML API 请求 URL 从 Cloud Storage 获取包含 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]

storage.googleapis.com/[BUCKET_NAME]

[BUCKET_NAME].storage.googleapis.com

[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:

您的客户端应用程序 <->你的后端返回 Access-Control-Allow-Origin <->谷歌云

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

这篇关于如何在我的谷歌脚本中允许 CORS 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆