Google Caching and faking jsonp

In using Google Apps Script as a jSONP proxy, I covered how you could use GAS to get over javaScript same domain restrictrictions. If you need to access jSon data from javaScript, jQuery and so on, and your script is running locally or the data in a different domain than your script, unless the data hosting platform supports jsonp, you are going to find it difficult to acces that data from javaScript.

You can access this as in this jQuery example.
$.getJSON(proxyJsonp + “?url=” + url + “&callback=?”, null, function(data) {

  … deal with data returned in  … jQuery.parseJSON(data.results)

});

where

  • url is the address of your (non-jsonp capable) data .. for example http://dl.dropbox.com/u/14196218/Share/somedata.json
  • proxyJsonp is the shared url of your Google Apps script jsonp faker. There’s one at https://script.google.com/a/macros/mcpher.com/s/AKfycbzGgpLEWS0rKSBqXG5PcvJ7Fpe02fvGqiCqq54SVQmBJSpy_6s/exec

Caching with Google Apps Script
Another benefit of ‘faking’ jSonP by using Google Apps Script is that you can use the Google Caching service. This means that multiple urlFetches() to slower hosting platforms can be avoided, since the GAS jsonP faker will cache data with the same URL. For details of how this works, see using Google Cache.

(function() {
var po = document.createElement(‘script’); po.type = ‘text/javascript’; po.async = true;
po.src = ‘https://apis.google.com/js/plusone.js’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(po, s);
})();

Author: brucemcp

Leave a Reply

Your email address will not be published. Required fields are marked *