0

I have a web page made with HtmlServices. This page has a form I want to submit to a GAS web app made to behave as a web service.

When I use google app script UrlFetch to call my web service from my first GAS app, I very often get a timeout. Unfortunately we cannot set the GAS UrlFetch timeout value which I think is around 10s. 10s is not enough for a GAS app to copy a file, open/edit a spreadsheet and send an email!

So I decided to use Jquery and do an ajax post (because I can set the timeout value) within my web page built with HtmlServices. (so my page is sanitized by Google Caja). Jquery is said to be supported by Caja.

But I noticed that the Ajax URL is always rewritten to be the first web app URL (the url I want to post to is changed by Caja I assume). Seems to me that Google's Caja is preventing that Ajax call. I could not find anything on Caja / Ajax post limitations within a GAS HtmlService.

Would you have suggestions on how to call a web service from a GAS Web App, without having the limitation of the GAS timeout?

Rubén
  • 34,714
  • 9
  • 70
  • 166
VanacK
  • 549
  • 1
  • 5
  • 18
  • To clarify - you have a web app built on Apps Script and a service built on Apps Script. You want to have the web app submit data to the service? Does that sound right? – Arun Nagarajan Nov 02 '12 at 18:18
  • Yes, and its for very good reasons as both are not running on the same set of sharing permissions! One runs as the user accessing the page, the other runs as me and do stuff for me. – VanacK Nov 02 '12 at 18:45
  • I have never tried this, but it's a good usage indeed. Have you tried plain javascript? jQuery and Apps Script are not playing nice lately. There's an AJAX example [here](http://vanilla-js.com/). – Henrique G. Abreu Nov 02 '12 at 20:06

3 Answers3

2

The best suggestion I can give for right now is to use JSONP instead of Ajax to make the call. You should not see timeouts in that case, and it should work fine.

Corey G
  • 7,754
  • 1
  • 28
  • 28
0

Couple of things

  1. AJAX calls (with jQuery or vanilla JS) from a web app deployed on script.google.com to a content service deployed on script.google.com does not work. I've confirmed this and I believe this is a security restriction. I tried a few different workarounds and this looks like there is no short term options.

  2. Regarding the server side option with UrlFetchApp. I believe the timeout is actually 30 seconds. However, that might still not be enough and it looks you are running into frequently enough.

So basically the 3rd (less optimal) option that I would recommend is a "queue" based approach. In this approach - have your HTML web app call a ScriptDb queue. Then you can have a timed trigger (every minute or every hour) that runs as you to perform the requisite operations. Just share the same script library between both scripts so you can share the script DB reference for the queue.

Arun Nagarajan
  • 5,547
  • 1
  • 22
  • 19
  • Since we're going into the workaround path, there's one I use that is to create a Google Form and set a script to run onFormSubmit and the external script "calls" the other by submitting a form (possibly with parameters). And since both scripts are yours, the form one can "answer" the original by accessing its scriptdb (as a library) and writing to it. – Henrique G. Abreu Nov 03 '12 at 10:34
0

According to this answer from Eric Koleda, Caja was removed from Google Apps Script, but still the client-side code passed to the HtmlService is satinized some way and still there are restrictions. From https://developers.google.com/apps-script/guides/html/restrictions

HTTPS required for active content
"Active" content like scripts, external stylesheets, and XmlHttpRequests must be loaded over HTTPS, not HTTP.

Rubén
  • 34,714
  • 9
  • 70
  • 166