Cross-domain requests stopped working due to no `Access-Control-Allow-Origin` header present in the response

后端 未结 5 1430
迷失自我
迷失自我 2020-11-30 02:17

I have an error reporting beacon I created using Google Apps script and it is published to run as myself and to be accessible to "anyone, even anonymous," which

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-30 02:28

    When making calls to a contentservice script I always have sent a callback for JSONP. Since GAS does not support CORS this is the only reliable way to ensure your app doesn't break when x-domain issues arrive.

    Making a call in jQuery just add "&callback=?". It will figure everything else out.

     var url = "https://script.google.com/macros/s/{YourProjectId}/exec?offset="+offset+"&baseDate="+baseDate+"&callback=?";
     $.getJSON( url,function( returnValue ){...});
    

    On the server side

    function doGet(e){
     var callback = e.parameter.callback;
     //do stuff ...
     return ContentService.createTextOutput(callback+'('+ JSON.stringify(returnValue)+')').setMimeType(ContentService.MimeType.JAVASCRIPT);
    }
    

提交回复
热议问题