【问题标题】:Cross-Origin Read Blocking (CORB) problem while calling Google App script by Ajax通过 Ajax 调用 Google App 脚本时出现跨域读取阻塞 (CORB) 问题
【发布时间】:2021-02-14 21:26:10
【问题描述】:

我尝试通过 Ajax 从 .js 文件调用在 Google App 脚本上发布的脚本。 当我直接在浏览器上使用 url 时它工作得很好(我得到了 Json 答案),但是我在调​​用时不断收到“Cross-Origin Read Blocking (CORB) blocked cross-origin response with MIME type text/html”是从本地 Ajax 完成的。

知道如何解决这个问题吗??

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'
   });
}

【问题讨论】:

标签: ajax google-apps-script cross-origin-read-blocking


【解决方案1】:

要使用JSONP,您应该将网址放在脚本标签中:

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

callback 在您的 web 应用程序中的函数(即 answer)然后将被调用。在jquery中使用getScript()可以自动将script标签插入页面

Serving JSONP in your web pages。如果您在生产环境中使用JSONP,请确保您了解使用它的所有安全隐患。

【讨论】:

    【解决方案2】:

    无需定义dataType JsonP

    关于这个有一个类似的answer。您的 AJAX 请求正在尝试使用 Padding dataType: 'jsonp' 检索 JSON。

    根据Jquery Documentation > JSONP 所说的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.

    总结一下,把你的dataType改成json

    参考

    jQuery.getJSON()

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 2019-12-25
      • 2018-11-25
      • 1970-01-01
      • 2019-01-22
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多