如何让我对谷歌Apps脚本ContentService的XHR / Ajax请求的工作? [英] How do I make XHR/ajax requests against Google Apps Script ContentService work?
问题描述
我有一个简单的谷歌Apps脚本ContentService的发出类似世界,你好星期六2012年7月14日14点十七分21秒GMT + 1000(EST)的网址是<一个字符串href="https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec">https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec而它的开放匿名。随意打。在code是:
I have a simple Google Apps Script ContentService that emits a string like "Hello world Sat Jul 14 2012 14:17:21 GMT+1000 (EST)" The url is https://script.google.com/macros/s/AKfycbxbFFG95mi8PWVNCE8366XaxnXQrt6p7p3OWbclXch_bbWczQ/exec and it's open to anonymous. Feel free to hit it. The code is:
function doGet() {
var output = ContentService.createTextOutput()
.setMimeType(ContentService.MimeType.TEXT)
.setContent("Hello world " + new Date());
Logger.log(output.getContent());
return output;
}
在我访问的网址在浏览器中它返回的字符串作为预期(pass.png)。当我使用相同的URL在XHR(AJAX调用)没有一个空的错误。在Chrome浏览器的开发者工具的重定向是(取消)(fail.png)。这里是code重现失败:
When I visit the URL in a browser it returns the string as expected (pass.png). When I use the same URL in an XHR (ajax call) it fails with an empty error. In the developer tools in Chrome the redirect is "(canceled)" (fail.png) . Here is the code to reproduce the fail:
<!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调用从example.com一个普通的旧网页从谷歌的匿名Apps脚本ContentService的脚本获取内容
Direct request: XHR request: My question (hopefully specific enough): How do I make XHR calls from a plain old web page on example.com to GET content from anonymous Google Apps Script ContentService scripts?
推荐答案
我不知道这是目前可能的。我们认为JSONP方法(没有工作,我已经测试过),但我不认为做对ContentService的一个XHR被测试过。我们很可能需要建立CORS头这一点。请您到问题跟踪功能请求,我们将看看是否是可以做到的。
I am not sure this is currently possible. We considered the JSONP method (which does work; I've tested it) but I don't think making an XHR against ContentService was ever tested. We'd probably need to set up CORS headers for this. Please file a feature request on the issue tracker and we'll see if it can be done.
这篇关于如何让我对谷歌Apps脚本ContentService的XHR / Ajax请求的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!