-1

I try to call a script published on Google App script from a .js file by Ajax. It works very fine when I use the url directly on the browser (I get the Json answer), however I keep getting a "Cross-Origin Read Blocking (CORB) blocked cross-origin response with MIME type text/html" when the call is done from Ajax locally.

Any idea how this could be fixed ??

function doGet(e) {
   Logger.log('I was called finaly');

   var data = "some data";
   var result = JSON.stringify({data: data});

   return ContentService
    .createTextOutput(e.parameter.callback + "(" + result + ")")
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
 }

let callGoogleScript = (user) => {
   console.log("callGoogleScript triggered");
   var link = "https://script.google.com/macros/s/123/exec";
   var url = link + "?callback=answer&name=";
   $.ajax({
     crossDomain: true,
     url: url + encodeURIComponent(user),
     method: "GET",
     dataType: 'jsonp'
   });
}
Thib
  • 173
  • 2
  • 13
  • Are these information useful for your situation? https://stackoverflow.com/q/62856630/7108653 https://gist.github.com/tanaikech/a72aab0242012362c46ec69031c720d5#ajax – Tanaike Nov 02 '20 at 08:15

2 Answers2

0

There's no need to define dataType JsonP

There's a similar answer about this. Your AJAX request is trying to retrieve a JSON with Padding dataType: 'jsonp'.

As per the Jquery Documentation > JSONP says If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

To sum up, change your dataType to json

Reference

jQuery.getJSON()

Jose Vasquez
  • 1,678
  • 1
  • 6
  • 14
0

To use JSONP, you should put the url in a script tag:

<script src = "https://script.google.com/macros/s/123/exec?callback=answer&name="></script>

callback function in your webapp(i.e., answer) will then be called. The script tag can automatically be inserted into the page in jquery using getScript()

See Serving JSONP in your web pages. Make sure you understand all the security implications of using JSONP, if you're using it in a production environment.

TheMaster
  • 45,448
  • 6
  • 62
  • 85