Since we are doing a cross origin JSON request, and GAS does not support CORS, we have to use JSONP. To know more about JSONP see JSONP and JSON and Google Apps Script webapps.
Here’s an example of a very simple non blocking JSONP implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function getData(siteCode, appUrl) { //create a script element to hold jsonp response var script = document.createElement('script'); // construct the url script.src = appUrl + "?" + [ "sitecode=" + siteCode, "page=" + encodeURIComponent(document.referrer), "callback=response" ].join("&"); document.body.appendChild(script); } function response(result) { // gets called when the jsonp script is loaded and executed if (!result.data ) { console.log ( 'error - no data(' + JSON.stringify(result) + ")"); } //do something with the data doIt(result.data); } |
See more like this in Displaying analytics data on site pages