Google Apps Script跨域请求停止工作 [英] Google Apps Script cross-domain requests stopped working

查看:214
本文介绍了Google Apps Script跨域请求停止工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google Apps脚本创建了报告灯塔的错误报告,并发布为以我自己的身份运行,并且可供任何人,甚至匿名访问,其中应该表示X域请求但是,我的浏览器现在指出没有 Access-Control-Allow-Origin 在代码发布到信标后,响应中的标题。

我在这里错过了什么吗?这个过去最近两个月前就开始工作了。只要GAS发布为公共访问,那么它 设置 Access-Control-Allow-Origin 标题。



在Google Apps脚本中:

Code.gs

 函数doPost(data){
if(data){
// Do Something
}
返回ContentService.createTextOutput({status:'okay'},ContentService.MimeType上传.json);
}

客户端:

脚本.js

  $。post(beacon_url,data,null,json); 


解决方案

在调用contentservice脚本时, JSONP的回调。由于GAS不支持CORS,这是确保您的应用在x域问题到来时不会中断的唯一可靠方法。



在jQuery中调用只需添加 &放大器;回调=。它会将其他所有内容都计算出来。

  var url =https://script.google.com/macros/s/ { YourProjectId} / EXEC偏移= +偏移+ &安培; baseDate = + baseDate + &安培;回调=?; 
$ .getJSON(url,function(returnValue){...});

在服务器端

 函数doGet(e){
var callback = e.parameter.callback;
// do stuff ...
return ContentService.createTextOutput(callback +'('+ JSON.stringify(returnValue)+')')。setMimeType(ContentService.MimeType.JAVASCRIPT);
}


I have an error reporting beacon I created using Google Apps script and it is published to run as myself and to be accessible to "anyone, even anonymous," which should mean that X-domain requests to GAS are allowed.

However, my browsers are now indicating there is no Access-Control-Allow-Origin header on the response after the code posts to the beacon.

Am I missing something here? This used to work as recently as two months ago. So long as the GAS was published for public access, then it was setting the Access-Control-Allow-Origin header.

In Google Apps Script:

Code.gs

function doPost(data){
  if(data){
        //Do Something
  }
  return ContentService.createTextOutput("{status:'okay'}", ContentService.MimeType.JSON);
}

Client Side:

script.js

$.post(beacon_url, data, null, "json");

解决方案

When making calls to a contentservice script I always have sent a callback for JSONP. Since GAS does not support CORS this is the only reliable way to ensure your app doesn't break when x-domain issues arrive.

Making a call in jQuery just add "&callback=?". It will figure everything else out.

 var url = "https://script.google.com/macros/s/{YourProjectId}/exec?offset="+offset+"&baseDate="+baseDate+"&callback=?";
 $.getJSON( url,function( returnValue ){...});

On the server side

function doGet(e){
 var callback = e.parameter.callback;
 //do stuff ...
 return ContentService.createTextOutput(callback+'('+ JSON.stringify(returnValue)+')').setMimeType(ContentService.MimeType.JAVASCRIPT);
}

这篇关于Google Apps Script跨域请求停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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