微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过Ajax调用Google App脚本时,跨域读取阻止CORB问题

如何解决通过Ajax调用Google App脚本时,跨域读取阻止CORB问题

我尝试通过Ajax从.js文件调用在Google App脚本上发布的脚本。 当我直接在浏览器上使用url时,它工作得很好(我得到了Json的回答),但是当调用时,我不断收到“ MIME类型为text / html的跨域读取阻止(CORB)阻止跨域响应”是从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'
   });
}

解决方法

无需定义dataType JsonP

对此有类似的answer。您的AJAX请求正在尝试使用填充dataType: 'jsonp'检索JSON。

按照Jquery Documentation > JSONPIf 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()

,

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

<script src = "https://script.google.com/macros/s/123/exec?callback=answer&name="></script>
然后将调用您的Web应用程序中的

callback函数(即answer)。可以使用getScript()

将script标签自动插入jquery的页面中

请参见Serving JSONP in your web pages。如果您在生产环境中使用JSONP,请确保您了解使用Scenario: Conversion completes immediately Given ... When I start the conversion Then I should see the conversion is completed Scenario: Conversion will take a long time to complete Given ... When I start the conversion Then I should see the conversion is progressing 的所有安全隐患。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?