I'm trying to use ContentService, to serve JSONP in a webpage with Google Apps Script, following the samples provided in this link under "Serving JSONP in Web Pages" sub-topic.
GS code:
function doGet(request) {
var events = CalendarApp.getEvents(
new Date(Number(request.parameters.start) * 1000),
new Date(Number(request.parameters.end) * 1000));
var result = {
available: events.length == 0
};
return ContentService.createTextOutput(
request.parameters.prefix + '(' + JSON.stringify(result) + ')')
.setMimeType(ContentService.MimeType.JSON);
}
HTML/JS Code:
<script src="https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert"></script>
I published it, by deploying the GS code as webapp
, with the following parameters:
- Executing the app as: User accessing the web app
- Who has access to the app: Anyone
Unfortunately, whenever I try to run/open the HTML file containing the code, I got the following error in browser's console, and nothing happens:
How to run this sample provided in Google Apps Script link without errors ?
[OPTIONAL] Investigating the issue in details:
The error found in resources files generate by the browser is as follows:
And the browser translated DOM is as follows:
<html>
<head>
<script src="https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert">
</script>
</head>
<body></body>
</html>
The generated link, in my case is:
"https://script.google.com/macros/s/AKfycbwZCY0qrZ09szGvKOttA30IaJkdMAZrh_oNnvv0qzCqFWyuO5Wc/exec?start=1325437200&end=1325439000&prefix=alert"
If this link is placed in browser address bar, it shows the following message:
which means that the returned authorization response wasn't able to be displayed on browser and being translated as valid DOM.
How to overcome this issue ?