我有一个简单的Google Apps脚本ContentService,它发出一个字符串,如“Hello world Sat Jul 14 2012 14:17:21 GMT + 1000(EST)”,网址是https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec,它对匿名开放。随意点击它。代码是:如何针对Google Apps Script ContentService制作XHR/ajax请求?
function doGet() {
var output = ContentService.createTextOutput()
.setMimeType(ContentService.MimeType.TEXT)
.setContent("Hello world " + new Date());
Logger.log(output.getContent());
return output;
}
当我在浏览器中访问URL时,它返回字符串(pass.png)。当我在XHR(ajax调用)中使用相同的URL时,它会失败并出现空错误。在Chrome中的开发者工具中,重定向是“(取消)”(fail.png)。这里是重现故障代码:
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc() {
xhr=new XMLHttpRequest();
xhr.onreadystatechange=function() {
if (xhr.readyState==4 && xhr.status==200) {
document.getElementById("myDiv").innerHTML=xhr.responseText;
}
};
xhr.open("GET","https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec",true);
xhr.send();
}
</script>
</head>
<body>
<h2>Using the XMLHttpRequest object</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">Get Content via XHR</button>
</body>
</html>
直接请求: XHR请求: 我的问题(希望是不够具体):如何使从例如一个普通的旧网页XHR调用.com从匿名Google Apps脚本ContentService脚本获取内容?
....同样的问题....有趣。 – 2016-02-13 17:39:36
[Google Apps脚本跨域请求停止工作]的可能重复(http://stackoverflow.com/questions/29525860/google-apps-script-cross-domain-requests-stopped-working) – 2017-02-13 03:05:05