如何解决通过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 > 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
参考
,要使用JSONP
,应将网址放在脚本标签中:
<script src = "https://script.google.com/macros/s/123/exec?callback=answer&name="></script>
然后将调用您的Web应用程序中的 callback
函数(即answer
)。可以使用getScript()
请参见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 举报,一经查实,本站将立刻删除。