简体   繁体   English

使用 JS 和 Google Apps 脚本获取 CORS

[英]Getting CORS using JS and Google Apps script

Im using Google Apps Script, and Im just trying to connect from the javascript in the front end to the Google Script but I keep getting CORS errors.我正在使用 Google Apps 脚本,我只是尝试从前端的 javascript 连接到 Google 脚本,但我不断收到 CORS 错误。

Access to XMLHttpRequest at 'https://script.google.com/macros/s/KEY/exec' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS 策略已阻止从源“null”访问“https://script.google.com/macros/s/KEY/exec”处的 XMLHttpRequest请求的资源。

In the JS I have this在 JS 我有这个

  var email = $('#ID_EMAIL').val();

    $.post("https://script.google.com/macros/s/KEY/exec",
        {
          email: email
        },
        function (data) {
            console.log("success");
        }, 'json');

and in the GAS并在 GAS 中

function doPost(e) {
  return "hello";
  }

I dont know why i keept getting the error if it just a basic code.如果只是基本代码,我不知道为什么我会不断收到错误消息。

I have publish the google sheet as a web page, and the GAS as a web applications public to anyone.我已将 google 表格发布为 web 页面,并将 GAS 作为 web 应用程序向任何人公开。

I dont know what Im missing我不知道我错过了什么

This was happening to me not to long ago, and it was driving me crazy.这件事不久前发生在我身上,让我发疯。 Hopefully I can spare you some of that pain.希望我能减轻你的痛苦。 The ultimate problem (in my opinion) is that the error message is not actually helpful because it sends you down a CORS rabbit hole that is not actually useful.最终的问题(在我看来)是错误消息实际上没有帮助,因为它会将您发送到一个实际上没有用的 CORS 兔子洞。

For the doPost / doGet methods to work as expected, you have to return a ContentService object.要使 doPost / doGet 方法按预期工作,您必须返回ContentService object。 So, if you change your doPost to something like the following, you should be good:因此,如果您将 doPost 更改为以下内容,您应该会很好:

function doPost(e) {
  return ContentService.createTextOutput("hello");
  }

Alternatively, if you want to return a json:或者,如果您想返回 json:

return ContentService
    .createTextOutput(JSON.stringify({
      "result": "success",
      "success": "hello"
    }))
    .setMimeType(ContentService.MimeType.JSON);

Make sure to redeploy your webapp after making changes.确保在进行更改后重新部署您的 webapp。

This doesn't appear to be the case for you, but, it's worth mentioning that if there is any fatal error in the google script web app, you will see that CORS error (at least in my experience).对您来说似乎并非如此,但是值得一提的是,如果 google 脚本 web 应用程序中有任何致命错误,您将看到 CORS 错误(至少在我的经验中)。 So for example, once upon a time, I was seeing that error because, although I was returning a ContentService object, I was calling a variable that I had not yet defined, so the script was erroring out before hitting the return statement.例如,曾几何时,我看到了这个错误,因为虽然我返回了一个 ContentService object,但我调用了一个尚未定义的变量,所以脚本在返回语句之前出错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Google Apps脚本上使用Google Script API获取开发人员元数据 - Getting Developer Metadata using Google Script API on Google Apps Script 登录并抓取一个使用 CORS(AWS) 和 JS 以使用 Google Apps 脚本填充自身的站点 - Login and scrape a site that uses CORS(AWS) and JS to populate itself with Google Apps Script 使用Google Apps脚本获取已发送文件夹中的未读数量 - GMail - Getting unread count in Sent Folder using Google Apps Script - GMail Google网站上的Apps脚本小工具开始引发CORS错误 - Apps Script Gadget on Google Site started throwing CORS errors 如何使用 Google Apps Script 中的 google.script.run 将 JS 对象传递给服务器函数? - How to pass a JS object to a server function using google.script.run in Google Apps Script? 在Google Apps脚本中使用jQuery - Using jquery in google apps script 在Google Apps脚本中使用Javascript - Using Javascript in Google Apps Script Google Apps 脚本获取错误的持续时间值 - Google apps script getting wrong duration values 如何将 sessionStorage 与 JS 和 Google Apps Script 一起使用? - How use sessionStorage with JS and Google Apps Script? 在Google工作表中将JS代码调整为Apps脚本 - Adjusting JS code to Apps Script in Google sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM