小编Pet*_*ann的帖子

如何针对Google Apps Script ContentService生成XHR/ajax请求?

我有一个简单的谷歌Apps脚本是contentService的发射类似"世界,你好星期六2012年7月14日十四时17分21秒GMT + 1000(EST)"的网址是一个字符串https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec,它对匿名开放.随意点击它.代码是:

function doGet() {
  var output = ContentService.createTextOutput()
      .setMimeType(ContentService.MimeType.TEXT)
      .setContent("Hello world " + new Date());
  Logger.log(output.getContent());
  return output;
}
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中访问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>
Run Code Online (Sandbox Code Playgroud)

直接要求: DIrect requestpass.png XHR请求: 在此输入图像描述 我的问题(希望足够具体):如何从example.com上的普通旧网页进行XHR调用,从匿名Google Apps脚本ContentService脚本获取内容?

ajax xmlhttprequest cors google-apps-script

12
推荐指数
1
解决办法
6164
查看次数

标签 统计

ajax ×1

cors ×1

google-apps-script ×1

xmlhttprequest ×1