由于响应中不存在“ Access-Control-Allow-Origin”标头,因此跨域请求停止工作 [英] Cross-domain requests stopped working due to no `Access-Control-Allow-Origin` header present in the response
问题描述
我有一个错误报告信标,该信标是我使用Google Apps脚本创建的,并发布为自己运行,并且任何人,甚至匿名都可以访问。
但是,我的浏览器现在指示没有 Access-Control -在代码发布到信标后,在响应上添加-Allow-Origin
标头。
我在这里缺少什么吗?这曾经在两个月前生效。只要GAS公开发布以供公众使用,它就会设置 Access-Control-Allow-Origin
标头。
在Google Apps脚本中:
Code.gs
function doPost(data){
if(数据){
//做某事
}
返回ContentService.createTextOutput( {{status:'okay'},ContentService.MimeType.JSON);
}
客户端:
script.js
$。post(beacon_url,data,null," json");
在调用contentservice脚本时,我总是发送过JSONP的回调。由于GAS不支持CORS,因此这是确保您的应用在出现x域问题时不会中断的唯一可靠方法。
在jQuery中拨打电话只需添加 & callback =?。
var url = https://script.google.com/macros/s/ { YourProjectId} / exec?offset = + offset +& baseDate = + baseDate +& callback =?;
$ .getJSON(url,function(returnValue){...});
在服务器端
function doGet(e){
var callback = e.parameter.callback;
//做东西...
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.gsfunction 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);
}
这篇关于由于响应中不存在“ Access-Control-Allow-Origin”标头,因此跨域请求停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!