javascript - Enabling CORs on Safari -
i have made simple angularjs website. backend google sheet, not ideal it's free. have deployed following script webapp json google sheet: (this not code btw)
function doget(request) { var output = contentservice.createtextoutput(); var data = {}; var id = request.parameters.id; var sheet = request.parameters.sheet; var cell = request.parameters.cell; var ss = spreadsheetapp.openbyid(id); if (sheet) { if (cell) { data = ss.getsheetbyname(sheet).getrange(cell).getvalue(); } else { data[sheet] = readdata_(ss, sheet); } } else { // grab sheets except name // starts underscore ss.getsheets().foreach(function(osheet, iindex) { var sname = osheet.getname(); if (! sname.match(/^_/)) { data[sname] = readdata_(ss, sname); } }) } var result = cell ? data : json.stringify(data); var callback = request.parameters.callback; if (callback == undefined) { output.setcontent(result); output.setmimetype(cell ? contentservice.mimetype.text : contentservice.mimetype.json); } else { output.setcontent(callback + "(" + result + ")"); output.setmimetype(contentservice.mimetype.javascript); } return output; } function readdata_(ss, sheetname, properties) { if (typeof properties == "undefined") { properties = getheaderrow_(ss, sheetname); properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); }); } var rows = getdatarows_(ss, sheetname); var data = []; (var r = 0, l = rows.length; r < l; r++) { var row = rows[r]; var record = {}; (var p in properties) { record[properties[p]] = convert_(row[p]); } data.push(record); } return data; } function convert_(value) { if (value === "true") return true; if (value === "false") return false; return value; } function getdatarows_(ss, sheetname) { var sh = ss.getsheetbyname(sheetname); return sh.getrange(2, 1, sh.getlastrow() - 1, sh.getlastcolumn()).getvalues(); } function getheaderrow_(ss, sheetname) { var sh = ss.getsheetbyname(sheetname); return sh.getrange(1, 1, 1, sh.getlastcolumn()).getvalues()[0]; }
in angular app have simple service uses angular http access webapp it's url , id of spreadsheet:
app.service('googlesheets', function($http) { return { getsheet: function(url, id) { return $http({ url: url, method: 'get', params: {id: id} }).then(function(data) { return data.data; }); } }; });
this works on chrome, opera, firefox, , microsoft edge. when tried use on safari received following error:
xmlhttprequest cannot load http://<myserveraddress>/webmethods.asmx/method. cannot make requests null
after research discovered because of way safari handles cors. read 1 question mentioning changing header involving allow-access-control-origin not know how this. kind of thing safari doesn't support or there can do? know divide spreadsheet pages separate files , access them simple requests parse html, bulkier , love avoid this.
Comments
Post a Comment